Read Code Fast

Lesson 17 Reading a diff

6 min read · Python · Quiz

diff anatomy, and the question "what behaviour changed?"

What you'll learn

  • Read the parts of a diff: -, +, context and the @@ header
  • Say in words what behaviour a change alters
  • Ask what is missing from the diff

Most AI work reaches you as a diff, not as a file. A diff is short, but it hides the one thing you need: what the program does differently now.

The idea

A diff is a list of edits. Lines starting with - were removed, lines starting with + were added, and lines starting with a space are unchanged context. The @@ -1,7 +1,4 @@ line says where: 7 old lines starting at line 1 became 4 new lines.

Don't review lines. For every hunk, ask one question: what behaviour changed? Say it in words: "an unknown code used to raise an error, now it gives no discount." Then ask a second one: what is not in the diff? A test, a caller, an error path.

discounts.py (diff)
1--- a/app/discounts.py2+++ b/app/discounts.py3@@ -1,7 +1,4 @@4 RATES = {"SPRING": 0.1, "VIP": 0.25}5 6 def apply_discount(order, code):7-    rate = RATES.get(code)8-    if rate is None:9-        raise ValueError(f"Unknown code: {code}")10-    return order["total"] * (1 - rate)11+    return order["total"] * (1 - RATES.get(code, 0))
  1. Line 3 The hunk header: 7 old lines became 4 new lines. Three lines vanished, so something was deleted.
  2. Line 8 Removed: the check that raised on an unknown code. Deleted lines are where behaviour quietly disappears.
  3. Line 11 Added: a missing code now means a rate of 0. An unknown code no longer fails, it silently gives no discount. A typo like "vip" now costs the customer money.
  4. Line 6 Unchanged context. Look at what is not in the diff: no test and no caller changed. Was the error tested? Does any caller catch ValueError?

Take away

  • - was removed, + was added, a space is unchanged context.
  • Review behaviour, not lines: say in words what is different now.
  • Then ask what is missing from the diff: tests, callers, error handling.

Quiz: test yourself

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