Read Code Fast

Lesson 4 Reading order

5 min read · Python · Quiz

tests, then types and signatures, then core logic, then the rest

What you'll learn

  • Read a change in order: tests, signatures, core logic, the rest
  • Read a test as a sentence of the spec
  • Spot what the tests leave out and tests that were bent to fit

Where you start reading decides how much you understand. Line 1 of the biggest file is almost always the wrong place.

The idea

Read in this order. 1. Tests: they show what the code is meant to do, with real examples. 2. Types and signatures: the names, inputs and outputs, a table of contents. 3. The core logic: the one function that does the real work. 4. The rest: helpers, glue and config, only as far as you need.

Each step makes the next one cheaper: examples, then the outline, then the details. One warning. If the AI wrote the tests too, they show what the AI believes the code does. Read them as a claim, and look for the cases they leave out.

test_discount.py
1from discount import apply_discount234def test_save10_takes_10_percent_off():5    assert apply_discount(100, "SAVE10") == 90678def test_unknown_code_changes_nothing():9    assert apply_discount(100, "NOPE") == 100101112def test_total_never_goes_below_zero():13    assert apply_discount(15, "TAKE20") == 0
  1. Line 1 The first thing to take from a test file: the name and the inputs, apply_discount(amount, code).
  2. Line 4 Rule 1, as an example: a 100 order with SAVE10 is 90.
  3. Line 8 Rule 2: a code that doesn't exist is not an error, the amount is unchanged.
  4. Line 12 Rule 3: the floor is zero. Now ask what is not here: a lowercase code, a negative amount, an empty code.

Take away

  • Order: tests, then types and signatures, then core logic, then the rest.
  • Tests are examples of the intended behaviour; read them first.
  • Tests the AI wrote are a claim: look for what they don't cover, and for tests that were edited to pass.

Quiz: test yourself

7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.