Read Code Fast
Lesson 1 The three passes
headline in 5 seconds, shape in 30, then close attention only where it can hurt
What you'll learn
- Skim a function in three passes instead of reading it line by line
- Say what a function does from its name, inputs and return value
- Know which few lines deserve the close reading
An AI just handed you a 40-line function. Reading it from the top, line by line, is the slowest way to review it and the least likely to find the bug.
The idea
Skim in three passes. Pass 1, the headline (5 seconds): read only the name, the inputs and what comes back, and say what the function does in one sentence. Pass 2, the shape (30 seconds): see the blocks (a loop, a guard, a call to the outside world) and give each one a word. Pass 3, the fine print: read closely only the lines that can hurt: money, time, user input, the network, the disk, anything deleted.
Most lines are plumbing and can't be wrong in an interesting way. The bug is almost always in a few lines, and the first two passes tell you which.
1def invoice_total(items, discount_pct):2 total = 03 for item in items:4 total += item["price"] * item["qty"]56 discount = total * discount_pct / 1007 return total - discount
- Line 1 Pass 1: the name, the inputs and the output. "The total of an invoice after a percentage discount."
- Line 3 Pass 2: a loop that adds up price times quantity. Name it: sum.
- Line 6 Pass 2: apply the discount. Name it: discount.
- Line 7 Pass 3: this is money. What if
discount_pctis 110? What ifitemsis empty? Close reading starts here.
Take away
- Three passes: headline, shape, fine print.
- Name each block with one word before you read its lines.
- Spend your attention on the few lines that touch money, time, input or the outside world.
Quiz: test yourself
7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.