๐ WAYPOINT LESSON
A diff against intention
โญโญโญ advancedโณ 10 min read๐ Lesson 217 of 256
Spec first, code second: the bug is always 'code does X, spec says Y' โ trace the cheapest failing case.
Debugging is a diff against intention. The broken method was written by someone competent; the bug is ONE decision, and the symptom usually appears far from the cause. The exam-style procedure:
- Read the spec first, the code second. The bug is always "code does X, spec says Y" โ you cannot see the diff without both sides.
- Pick the cheapest failing case from the examples. Trace it by hand through the code โ not the spec, the CODE.
- Locate the first line where trace and spec disagree. That line is the bug; everything after it is consequence.
- Name the bug class before fixing: boundary, wrong accumulator, inverted condition, missing reset, wrong tie-break. Naming prevents the "fixed it by accident" failure.
The classic subtle bugs, ranked by exam frequency: off-by-one loop
bounds, < where the spec said <=, forgetting to reset a
run-counter, >= where a tie-break demanded >, and mutating
while iterating forward.