Boss lesson · Unit 04
Code nobody wants to touch
Find a seam, pin it, change one thing, repeat
What you practise
- Find a seam, pin behaviour, change one thing, repeat
- Replacing a part at a time
- Knowing when it is good enough
Try a question from this lesson
Pick one
Forty places call this old function, and a promotion needs a change to it. You want to record what it does today first. What makes that awkward?
def print_invoice(lines):
t = 0
for x in lines:
t = t + x
if t > 100:
t = t - 10
print("INVOICE", t)function printInvoice(lines) {
let t = 0;
for (const x of lines) t = t + x;
if (t > 100) t = t - 10;
console.log("INVOICE", t);
}function printInvoice(lines: number[]) {
let t = 0;
for (const x of lines) t = t + x;
if (t > 100) t = t - 10;
console.log("INVOICE", t);
}Correct answer
Its result goes to the screen; nothing is returned
Why
A pin compares a value, and this function gives none back: the amount is worked out and printed in one breath. The calculation is the part worth pinning. The first step is to pull the two apart.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Find a seam
A seam is a place where a part can be split off without changing what the program does.
Here the seam runs between working out and printing. The calculation becomes a function that returns a value you can pin. The old name stays, as a thin wrapper, so not one of the forty callers has to change today.
def total(lines):
t = 0
for x in lines:
t = t + x
if t > 100:
t = t - 10
return t
def print_invoice(lines):
print("INVOICE", total(lines))function total(lines) {
let t = 0;
for (const x of lines) t = t + x;
if (t > 100) t = t - 10;
return t;
}
function printInvoice(lines) {
console.log("INVOICE", total(lines));
}function total(lines: number[]): number {
let t = 0;
for (const x of lines) t = t + x;
if (t > 100) t = t - 10;
return t;
}
function printInvoice(lines: number[]) {
console.log("INVOICE", total(lines));
}10 more exercises in the app
- 3× type the answer
- 1× order the lines
- 3× multiple choice
- 1× fill the blanks
- 1× true or false
- 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.