Stop fixing bugs by guessing and patching symptoms; instead, trace each bug to its root cause and verify the fix against the actual failure. This method works for any software bug, from a typo to a logic error, and it prevents the same issue from reappearing.
The usual way fails because you fix what you think is wrong, not what is actually wrong. You change a line, the error disappears, but you never confirm why it disappeared. The better way is to reproduce the bug, isolate the cause, apply a minimal fix, and test that the fix holds.
Why Quick Patches Keep Failing
Quick patches treat the symptom, not the disease. You might add a null check, but the real problem is that the data is empty in the first place. You might increase a timeout, but the real problem is a slow database query. The bug returns when the conditions change, because the underlying cause is still there.
| Symptom | Cause | Fix |
|---|---|---|
| App crashes on empty input | Missing validation before use | Add validation at the entry point, not just where the crash occurs |
| Data saved but not shown | Cache not invalidated | Clear or update the cache after write operations |
| Error only on some machines | Environment-specific configuration | Compare configuration files across environments |
| Fix works once, then breaks again | Fix addresses a side effect, not the root cause | Trace the data flow from the original source |
Three specific mistakes cause most recurring bugs. First, changing code without reproducing the bug first—you might be fixing the wrong thing. Second, applying a fix without understanding why it works—you might be masking the issue. Third, not testing the fix in the same conditions as the bug—you might be fixing a different scenario.
Reproduce the Bug Before You Touch Code
You cannot fix what you cannot see. Reproduce the bug reliably before you change anything. Write down the exact steps that trigger it, including the input data, the environment, and the sequence of actions. If you cannot reproduce it, you do not yet know what you are fixing.
- Write the steps in a note or issue tracker, with the exact input and expected vs. actual output.
- Run the steps three times to confirm the bug appears consistently.
- If the bug is intermittent, add logging around the suspected area to capture the state when it fails.
- Note the time, the user, and any recent changes that might correlate with the bug.
Reproducing the bug gives you a baseline. After you apply a fix, you can run the same steps and see if the bug is gone. Without a baseline, you are guessing.
Trace the Root Cause Step by Step
Once you can reproduce the bug, trace the data flow from the start to the point of failure. Read the code like a map. Follow the variable from its origin to where it causes the error. Do not assume—verify each step.
- Start at the function that throws the error or returns the wrong result.
- Look at the inputs to that function. Are they what you expect? Add a temporary print or log to see the actual values.
- Move backward through the call stack, checking each function that transforms the data.
- When you find a step where the data becomes unexpected, that is your root cause.
- Ask why that step produced the wrong result. Is it a logic error, a missing null check, a wrong assumption about the data format?
For example, if a date displays as 1970, the root cause might be a timestamp that is zero. The fix is not to format the date differently; it is to find why the timestamp is zero. Trace back to where the timestamp is set, and you will find the real issue.
Apply the Minimal Fix That Addresses the Cause
The smallest fix that corrects the root cause is the best fix. It reduces the chance of introducing new bugs and makes the change easy to review. Do not refactor unrelated code while you are fixing a bug. Do not add features or improve style. Focus on the one thing that causes the failure.
- Write the fix as a single change, such as adding a null check, correcting a condition, or changing a default value.
- Explain to yourself (or a colleague) why this change fixes the root cause, not just the symptom.
- Apply the change, then run the reproduction steps from earlier. The bug should be gone.
- Run the related tests for the module to ensure you did not break anything else.
- If the bug is not gone, revert the change and continue tracing—do not pile on another patch.
Sometimes the minimal fix is to remove a line, not add one. For example, if a variable is overwritten by mistake, deleting the overwrite might be the fix. Keep the change as small as possible.
Verify the Fix in the Same Conditions and Then Watch for Regression
After the fix, run the reproduction steps again. If the bug does not appear, you have verified it. But you are not done. Run the surrounding tests, and think about edge cases that might break. A fix that works for one input might fail for another.
- Run the exact reproduction steps three times to confirm the bug is gone.
- Run the unit tests for the changed function and its callers.
- Test with boundary values: empty, null, maximum, negative, or unexpected types.
- If the bug involved user input, test with malformed input as well as valid input.
- Commit the fix with a clear message that describes the root cause and the change.
A good fix lasts as long as the code remains unchanged. If the same bug appears again later, it usually means the root cause was not fully addressed, or a new change reintroduced it. When that happens, repeat the process: reproduce, trace, fix, verify.
Build Habits That Prevent Bad Fixes
Bad fixing habits are not just about the moment of fixing; they are about the routine around it. You can replace them with habits that make good fixes automatic.
- Always write a failing test before fixing a bug, if the codebase allows it. The test reproduces the bug and verifies the fix.
- Use version control meaningfully. Commit the reproduction steps and the test with the fix, so future you knows why the change was made.
- When you feel the urge to “just try something,” stop. Write down the hypothesis and test it deliberately instead.
- After a fix, review your own diff. Ask: does this change address the root cause? Could it break anything else?
These habits slow you down in the moment but save hours later. A bug fixed properly stays fixed. A bug patched quickly often returns, and you will have to fix it again, with more context missing.
Related
- Fixing a Wooden Wick That Keeps Going Out
- Why Perfectionism Is Bad for You
- Is Yoga Bad for the Environment?
General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more
Spotted an error? Tell us and we will correct it (corrections policy).