๐ WAYPOINT LESSON
Localize by Bisection
โญโญโญ advancedโณ 12 min read๐ Lesson 237 of 281
A failing code is a function with a broken region: find it by halving suspects, not by rereading.
Localize by Bisection
When your code passes the sample and dies on the hidden test, rereading is the slowest tool. Instead, bisect the space of suspects:
- Bisect the input. Take the smallest failing family you can imagine (n = 1? 2? 3?). If nothing fails small, double until it does โ the first failing n brackets the bug's scale.
- Bisect the code path. Comment out a half (or force an early return with a known-good answer). Does the failure move? The bug lives in the half that flipped the outcome.
- Bisect the state. Print invariants at the midpoint (sortedness, sum, monotonicity). The first checkpoint whose invariant is false is upstream of the bug.
The catalog habit
Every located bug gets a category name (see the next lesson). Naming converts a one-off fix into a reusable detection: "this is an off-by-one-at-boundary; check both endpoints" is a class of future bugs caught before they cost a submission.
The counterexample bonus
The minimal failing input is not just diagnostic โ it is the test that pins the fix. Add it to your personal stress harness (Module 13 makes that harness systematic) before repairing, so the repair can be verified against the exact evidence.