Lesson 4 of 8

Code Comprehension · The method · Lesson 4 of 8

Narrow it down

From a failing test to the exact line: read the symptom, guess the family, then check your guess cheaply. Change one thing at a time.

By Akshay, software engineer, India

On this page

The symptom names the suspect

Different bugs leave different marks. Before reading every line, ask what kind of bug produces exactly this output:

  • One item too many or too few: an edge, < vs <=, a range that’s off by one.
  • NaN, undefined, a null crash: empty or missing input, or a promise used as a value.
  • Right the first time, wrong the second: shared state, a mutable default, a cache.
  • Wrong only sometimes, or only under load: a race, or ordering between async calls.
  • A number slightly off, or 0: integer division, rounding, floats.
  • A value that changed “by itself”: two references to one object.

Check the guess cheaply

A hypothesis is only useful if you test it. The cheapest checks, in order:

  1. Re-read the suspect lines with the failing input in mind.
  2. Print at a boundary: the value going into a function and coming out. If it’s right going in and wrong coming out, the bug is inside.
  3. Halve the path: print in the middle of the flow. Wrong there? The bug is in the first half. Right? The second. Like binary search, through the code.
  4. Read the stack trace from the top, and start at the first line that is your code, not the library’s.

In a real codebase, git bisect does the same halving through history: it finds the commit that brought the bug in.

One change at a time

Change one thing, run the test, look. If you change three things and it passes, you don’t know which one mattered, and two of your changes may have broken something else. Undo experiments that didn’t help, so the final fix is only the change that matters.

Remember
  • Read the symptom and name the likely family before reading every line.
  • Test the guess cheaply: re-read, print at boundaries, halve the path.
  • Start at the first line of the stack trace that is your code.
  • Change one thing at a time, and undo what didn’t help.
Your progress stays on this device.