Bitpath

Lesson 1 · Unit 03

Data shapes

Group what travels together; make wrong values impossible

11 exercisesabout 3 min

What you practise

  • Parallel lists and bare strings against a record or class
  • Grouping what travels together
  • Making illegal states impossible to write down

Try a question from this lesson

Pick one

An order was marked as sent, with a slip of the keyboard. What does this print?

order = {"id": 7, "status": "shiped"}

if order["status"] == "shipped":
    print("on its way")
else:
    print("wait")
const order = { id: 7, status: "shiped" };

if (order.status === "shipped") {
  console.log("on its way");
} else {
  console.log("wait");
}
const order = { id: 7, status: "shiped" };

if (order.status === "shipped") {
  console.log("on its way");
} else {
  console.log("wait");
}

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Keep together what travels together

If two values only make sense as a pair, store them as one thing.

A list of records keeps each name next to its age. Sort it, filter it or pass it on, and the pairs survive, because there is no second list to forget.

people = [
    {"name": "Cy", "age": 40},
    {"name": "Ana", "age": 31},
    {"name": "Bo", "age": 25},
]
people.sort(key=lambda p: p["name"])
first = people[0]
const people = [
  { name: "Cy", age: 40 },
  { name: "Ana", age: 31 },
  { name: "Bo", age: 25 },
];
people.sort((a, b) =>
  a.name.localeCompare(b.name),
);
const first = people[0];
type Person = {
  name: string;
  age: number;
};

const people: Person[] = [
  { name: "Cy", age: 40 },
  { name: "Ana", age: 31 },
  { name: "Bo", age: 25 },
];
people.sort((a, b) =>
  a.name.localeCompare(b.name),
);
const first = people[0];

TypeScript notePerson[] gives every record the same checked shape: a record with a missing age, or with age as text, is a compile error.

Concept

Make wrong values impossible

Give a field with a few legal values a fixed set of them. A misspelling is then rejected at the spot where it is written, not discovered later in a branch that never ran.

More broadly: choose a shape in which wrong data cannot be written down. Checks you do not need cannot be forgotten.

from enum import Enum

class Status(Enum):
    NEW = "new"
    PAID = "paid"
    SHIPPED = "shipped"

status = Status.SHIPPED
const Status = Object.freeze({
  NEW: "new",
  PAID: "paid",
  SHIPPED: "shipped",
});

const status = Status.SHIPPED;
type Status = "new" | "paid" | "shipped";

const status: Status = "shipped";

Python noteStatus.SHIPED raises AttributeError the moment that line runs, instead of quietly creating a new status.

JavaScript noteA frozen object gives the spellings one home, but Status.SHIPED still reads as undefined without an error. Plain JavaScript cannot close this gap.

TypeScript noteA union of string literals needs no run-time code at all. Assigning "shiped" to a Status is a compile error, and the editor offers the three legal values.

8 more exercises in the app

  • 4× type the answer
  • 1× fill the blanks
  • 1× true or false
  • 1× multiple choice
  • 1× match the pairs

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy