Lesson 3 of 8

Code Comprehension · The method · Lesson 3 of 8

Reproduce it first

The step most people skip, and the one interviewers notice. Turn the report into a failing test before you change a line.

By Akshay, software engineer, India

On this page

Why first

If you can’t make the bug happen, you can’t know you’ve fixed it. You’ll change something, the symptom might go away by luck, and the real cause stays. Reproducing first also stops you “fixing” the wrong thing: half the time the code you suspected is fine.

Turn the report into a test

Reports are vague: “page 2 shows a duplicate”, “the total is sometimes wrong”. Turn it into an exact call with an exact expected answer:

// Report: "the last page is missing an item"
const items = [1, 2, 3, 4, 5];
assert.deepEqual(paginate(items, 2, 2), [3, 4]);   // fails: gives [3]

Now you have a red test. You’re done when it’s green, and it guards against the bug coming back.

Shrink the input

The report might come with 10,000 rows. Find the smallest input that still fails: halve it and check, keep the half that fails, repeat. A bug that shows up with 3 items is ten times easier to see than one with 300. Often the shrinking itself tells you the cause: “it only fails when two meetings touch”.

When you can’t reproduce it

Then something about your setup differs from where it fails. Check, one at a time: the input (exactly the same?), the environment (time zone, locale, version), the timing (does it need two requests at once?), and the state (does it only fail the second time, after a cache fills?). Say each check out loud. “It only happens on the second call” is already a strong clue: shared state or caching.

Remember
  • No fix before a failing test: it proves the bug and later proves the fix.
  • Turn the vague report into one exact call and one exact expected answer.
  • Shrink the input until the bug is obvious.
  • Can’t reproduce? Compare input, environment, timing and state.
Your progress stays on this device.