Reading at scale

Lesson 17 Reading a diff

6 min read · JavaScript · 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 @@ -2,10 +2,7 @@ line says where: 10 old lines starting at line 2 became 7 new lines.

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

discounts.js (diff)
1--- a/src/discounts.js2+++ b/src/discounts.js3@@ -2,10 +2,7 @@ const RATES = { SPRING: 0.1, VIP: 0.25 };4 5 function applyDiscount(order, code) {6   const rate = RATES[code];7-  if (rate === undefined) {8-    throw new Error(`Unknown code: ${code}`);9-  }10-  return order.total * (1 - rate);11+  return order.total * (1 - (rate || 0));12 }13 14 module.exports = { applyDiscount };
  1. Line 3 The hunk header: old lines 2 to 11 became new lines 2 to 8. Four lines vanished, so something was deleted.
  2. Line 7 Removed: the guard that threw on an unknown code. Deleted lines are where behaviour quietly disappears.
  3. Line 11 Added: a missing code now means a rate of 0. So an unknown code no longer fails, it silently gives no discount. A typo like "vip" now costs the customer money.
  4. Line 14 The diff ends here. No test and no caller changed. Was the throw tested? Does any caller catch it?

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.