Lesson 5 of 8

Code Comprehension · The method · Lesson 5 of 8

Fix it, then prove it

Fix the cause, not the symptom. Keep the change small. Then prove it: the failing test passes, the others still do, and the edges are covered.

By Akshay, software engineer, India

On this page

Fix the cause, not the symptom

If an average shows NaN, wrapping the display in || 0 makes the screen look right and hides the real problem: dividing by zero on an empty list. The cause-level fix decides what the average of nothing should be, and says so in the code. Ask: “if I make this change, is the bug gone, or just not visible here?”

Small and clearly right

The best fix is the smallest change that’s obviously correct. Don’t rewrite the function in your favourite style mid-interview: it’s slower, it risks new bugs, and it hides what you actually fixed. If you think a bigger change is better, say so and say why: “the minimal fix is this; in the real codebase I’d also…”.

Prove it

  1. Run the test that was failing. It passes.
  2. Run the other tests. They still pass.
  3. Add the edge cases the bug lived near: empty input, one item, the boundary itself, the largest value.
  4. Look for the same mistake nearby. Bugs come in families: if one loop was off by one, check its siblings.
Remember
  • Fix the cause; a fix that hides the symptom is a second bug.
  • Smallest clearly-correct change; mention bigger ideas separately.
  • Prove it: the failing test passes, the others still pass, edges are covered.
  • Check for the same bug in similar code nearby.
Your progress stays on this device.