Vigilant Software Blog
How to actually fix a bug
A new bug report landed on your desk this morning. Maybe it's from an customer in the field, maybe QA found it, or maybe you found it and reported it yourself. (You are using issue tracking software, right?)
If you're experienced in this sort of thing, your first step is usually to attempt to reproduce the issue. How can you know if you've fixed the problem without first seeing it fail? Maybe you can't reproduce it and need more information, or maybe it's actually working as intended and the report is mistaken.
For this story, let's assume the bug report is a real defect and you are able to replicate it. What's your next step?
If you said "write a test", you get a cookie. Write a test and watch it fail. (You have a test suite, right?) Do not pass go; don't start debugging, don't start hacking around in the code, and definitely don't commit anything.
Write a test, watch it fail.
Once you have a failing test, you are free to start changing things. Diagnose and fix the bug, re-run your test suite. Not only should all the old tests pass (verifying your change didn't break anything), but the new test should pass (proving your "fix" actually fixes something).
This approach accomplishes a few things:
- It forces you to stop and actually reproduce the issue.
- It ensures that you've done a before and after test, and that your change actually fixes the issue.
- Finally, it prevents this issue from re-appearing in the future - you get a regression test for free by following this process.
It's frustrating to squash a bug, only to have it reappear later, right? This is how you make sure they stay dead. It's actually a form of test-driven development, an approach that is well-suited to bug fixes.