Skip to content
mentoor

Topic 4 of 5 · 5 min read

Fixing root causes, not symptoms

Goal: Tell a root-cause fix from a symptom patch in a proposed diff and name the regression test that proves the fix.

A good fix removes the cause of the failure and comes with a test that fails without it. A symptom patch makes the error message go away and leaves the bug in place. AI assistants are quick to produce the second kind, because the quickest way to remove an error is often to hide it.

Check yourself · 1 of 3

Which change is most likely a symptom patch?

Choose one answer

Why it matters

A patched symptom comes back as a different bug, or corrupts data silently. A root-cause fix is smaller to maintain: the code is more correct, not more defensive.

How to do it

  1. Ask why, repeatedly. Start from the failure and keep asking: the order total was wrong, because a line had a null price, because the price lookup failed, because the product was deleted, because deletion does not check open carts. The fix belongs near the end of that chain, not at the first answer. Stop when the next answer is a decision or a design fact you can change.
  2. Write the regression test first. Use your minimal repro. It must fail now, for the reported reason.
  3. Make the smallest change that fixes the cause. Then run the test: it must pass. Run the surrounding suite too.
  4. Check for siblings. If one place had this bug, search for the same pattern elsewhere.
  5. Note the cause. One sentence in the commit message: why it broke, not only what changed.

Warning signs in a proposed fix

  • An empty catch, or a catch that only logs and continues.
  • A blind retry or a longer timeout, when the cause is not transient.
  • A null check that returns a default (0, "") and hides missing data.
  • A change to the test's expected value rather than the code.
  • sleep(...) to "fix" a race.
  • A fix at the call site when the function is the one that is wrong.

Before and after

Symptom patch for TypeError: price of undefined:

- const total = items.reduce((s, i) => s + i.product.price * i.qty, 0);
+ const total = items.reduce((s, i) => s + (i.product?.price ?? 0) * i.qty, 0);

Deleted products now count as free, and the customer is undercharged without any error. The root cause was that product deletion left items in open carts:

+ // deleteProduct: refuse while the product is in an open cart
+ if (await carts.hasOpenItem(productId)) throw new ProductInUseError(productId);

with a regression test that deletes a carted product and expects ProductInUseError.

What goes wrong

  • Fixing where it crashes, not where it starts.
  • Adding defensive code that makes bad data valid.
  • No regression test, so the bug returns in six months.
  • Fixing three things at once, so the real fix cannot be reviewed.

Examples

The retry that was not a fix

A job fails with a deadlock error. The proposed diff adds retry(3). Failures drop from 12 a day to 2, but the cause is two transactions locking the same rows in opposite order. The root-cause fix is to lock in a consistent order. Retry may stay as a safety net, but not as the explanation.

Fix the test or the code?

A test expects a discount of 10.00 and gets 9.99. The assistant edits the test to expect 9.99. Check the rule first: if the spec says round half up, the code is wrong and the test was right.

Key points

  • Ask why repeatedly until you reach a cause you can change, and fix there, not at the crash site.
  • Write the regression test first and watch it fail for the reported reason.
  • Swallowed errors, blind retries, default values and sleeps are symptom patches unless the cause is truly transient.
  • Changing a test's expected value to match the code is not a fix.
  • Search for the same pattern elsewhere and record the cause in the commit message.

Common mistakes

  • Accepting the first change that makes the error disappear.
  • Using try/catch, defaults or retries to hide a failure you do not understand.
  • Shipping a fix with no test that fails without it.
  • Editing a failing test to match new behaviour without checking which is correct.

Check yourself · 2 of 3

A failing test expects 10.00 and gets 9.99. An assistant edits the test to expect 9.99. What should you do?

Choose one answer

Check yourself · 3 of 3

Why write the regression test before the fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic