๐ WAYPOINT LESSON
First-error localization
โญโญ intermediateโณ 12 min read๐ Lesson 149 of 256
Restate, trace the failing case, fix the first divergence only.
The exam does not ask "is this code right?" โ it asks "which change makes it right?" and grades the diagnosis. The localization loop:
- Restate the contract in one sentence.
- Trace the smallest failing case โ the one the prompt shows failing (its examples ARE the failing tests).
- Find the FIRST statement where the state table diverges from what the contract needs. Everything before that statement is correct โ do not touch it.
- The fix is usually one token: a comparison operator, an
initialization value, a loop bound, a missing
this., an argument in the wrong slot. If your fix rewrites half the method, you mislocated the error.
Why "first" matters: a later correct-looking line that misbehaves is usually a victim of the earlier bug, not the bug. Fixing victims adds bugs; fixing the source collapses the cascade.
Worked micro-example โ contract: "sum of even elements":
int total = 0;
for (int i = 0; i <= arr.length; i++) { // victim: throws at the end
if (arr[i] % 2 == 0) { total += arr[i]; }
}
The exception at the LAST iteration tempts you to "fix the array".
The first divergence is the <= bound itself. One token: <.