Skip to content

Expressive data types

Learning objectives

  • Practice to use classes for expression
  • Practice to use classes for type safety
  • Understand difference between composition and aggregation
  • Practice to use composition for a 'has-a' relationship

What and why?

Data structure are 'ways to organize your data'.

  • Bad way: put all in one list

Good (😇) data structures:

  • Increase expressiveness
  • Bundles data that belongs together
  • Ensures correct state of the program

Increase expressiveness, in design

"A two-dimensional coordinate has a x and a y component"

classDiagram
  class Particle{
    -position
    -velocity
  }

Class diagram of a two-dimensional coordinate

Increase expressiveness, in code

a = get_a()
print(a)
print(type(a))
a <- get_a()
a
class(a)

Increase expressiveness, in code

class Coordinat:
    def __init__(self, any_x, any_y):
      self.x = any_x
      self.y = any_y
    def __repr__(self):
        return "Coordinat"
    def __str__(self):
        return "(" + str(self.x) + ", " + str(self.y) + ")"
def get_a():
    return Coordinat(3.14, 2.72)
a = get_a()
print(a)
print(type(a))
a <- get_a()
a
class(a)

Ah, it is a coordinat!

Exercise 1: design a struct (15 mins)

Learning objectives
  • to convince design is trickier than one thinks
  • to convince design has implications
  • to grow appreciation of classes

What are their elements? Which do you guess are structures? Were they?

  • A coordinate in 3 dimensions 😇
  • A velocity in two dimensions
  • A circle
  • A square