Master

Designing the values of a whole program

A small program becomes a design question once it has more than a few values: which values you keep, and which you work out when needed. Both choices are decisions, and the program is easier to change when they are made on purpose.

What you will see

9

About 2 min of reading

Try It

Design the values for a small task before coding it.

1

Store two facts and derive a third.

first = "Ada" last = "Lace" print(first + " " + last)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada Lace
2

Keep cents as the fact and show dollars as derived.

cents = 1250 print(cents / 100)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
12.5
3

Loop on a named state instead of a counted range.

remaining = 3 while remaining > 0: print("turn") remaining = remaining - 1

Before you run it, say out loud what it will print. Then open the answer.

What it prints
turn turn turn
4

Track a best so far, starting from nothing.

best = None for score in [4, 9, 7]: if best is None or score > best: best = score print(best)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
9
Starting from None rather than zero is what lets the first real value win, which is the pattern for any best-so-far search.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

You keep the facts in names, work out derived values when needed, and change only the facts as the program runs.
1 first and last are facts
↓
2 full is derived
↓
3 only facts change
↓
4 derived values follow
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

first = "Ada" sets nothing on screen
last = "Lace" sets nothing on screen
print(first + " " + last) Ada Lace
Why this works
Why this is the last lesson

Everything before this was about one value at a time. This is the point where values meet a program, and where the question stops being what the computer does and becomes what you chose to keep. That question is the one that scales for as long as you write programs.

When you have a choice

Facts versus derived

Split the values of a program into the ones that are facts and the ones that follow from facts. Only facts get changed. Every derived value is a function of the facts, so it can never disagree with them, and it can be recomputed whenever needed.

Examples

Keep one fact, derive the rest

first = "Ada"
last = "Lace"
full = first + " " + last
print(full)

first and last are the facts. full is worked out when needed. If a name changes, only the facts change, and every derived value follows.

The cost of storing both

cents = 1250
print(cents / 100)

Storing the dollars as well would mean two values that must agree. They can drift apart, and a program with drifting values has answers that contradict each other. Deriving removes the possibility of drift.

Name the state, not the step

remaining = 3
while remaining > 0:
    print("turn")
    remaining = remaining - 1

remaining says what is left, which is a fact a reader can check. A counter counting up to a limit says the same thing less directly. Naming the state is what makes a loop readable.

A record of decisions

best = None
for score in [4, 9, 7]:
    if best is None or score > best:
        best = score
print(best)

best holds the best so far, and None records that nothing has been seen. The loop only ever changes one value, so the whole program is one fact updated in place.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Text is numbers too
Previous Lesson
Text is numbers too