Advanced

Not repeating yourself

When the same three lines appear in several places, they are one function that was never named. Repeating yourself means changing the same thing in several places.

What you will see

Points: 4

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Replace a repeated shape with a function.

1

Print the same label twice.

print("Score: " + str(4)) print("Score: " + str(9))

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

What it prints
Score: 4 Score: 9
2

Name the shape and call it twice.

def score(n): return "Score: " + str(n) print(score(4)) print(score(9))

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

What it prints
Score: 4 Score: 9
3

Name a repeated action.

def save(name): print("saved " + name) save("map") save("score")

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

What it prints
saved map saved score
4

Change the label in one place and re-run.

def score(n): return "Points: " + str(n) print(score(4))

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

What it prints
Points: 4

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

Any block that appears more than once should be named once as a function, so a change happens in one place.
1 shape repeats
↓
2 function holds it once
↓
3 calls replace copies
↓
4 one edit changes all
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.

print("Score: " + str(4)) Score: 4
print("Score: " + str(9)) Score: 9
Why this works
One place to change

The value of a function is that its body exists once. Every rule that appears twice should exist once, so a change is one edit rather than a hunt.

Do not abstract too early

A function used once is a detour. Wait until the copy is real, or the shape is likely to change, before naming it.

Examples

Spot the copy

print("Score: " + str(4))
print("Score: " + str(9))

The shape repeats. Naming it once turns two copies into two calls of one function.

Name the shape

def score(n):
    return "Score: " + str(n)

print(score(4))
print(score(9))

The shape now lives in one place. Changing the label is one edit, and both uses follow.

Repeating steps, not text

def save(name):
    print("saved " + name)

save("map")
save("score")

The steps repeat, so they get a name. A function is the answer to any copy you can point at.

When not to abstract

print(1 + 2)
print(3 * 4)

Two lines that only look alike by chance should stay as they are. A name for something used once adds a jump for no gain.

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.

← Splitting a program into functions
Previous Lesson
Splitting a program into functions
Next Lesson
Optional arguments
Optional arguments →