Justin starts a blog

Text

How to fix a bug

I had a conversation with a developer today about proper debugging techniques. My approach is this:

1. Ensure that a bug has been filed with three sections: Steps to reproduce, expected behavior, and actual behavior.

2. Create an experiment that proves the issue. In situations where test-driven-development applies, create a new test case. In other cases, create a test page with two sections: One that demonstrates the issue, and one that acts as the control where the issue does not occur. Include a description of the issue and the link to your bug.

3. Stop. Take a deep breath. Slow down and begin to think logically and methodically. It’s important to act more like a scientist than like an engineer here, especially when you’re dealing with other people’s code.

4. Begin debugging by cutting the problem in half. Find a place in the code where you suspect things may have gone awry and place your probe on either side of it and test. Repeat until you’ve located where the issue is.

5. Now apply hypotheses until you’re confident that you understand the cause of the issue and the appropriate fix. If you make a change and you don’t understand exactly why it fixes the issue then you haven’t really fixed anything, you’ve potentially created other issues.

6. Once you’re confident that the bug is fixed properly, spend some time with older experiments (you’re keeping those around right?) making sure that your change did not introduce unexpected consequences. Ideally if you’re project is test driven you can do this automatically by running your test suite.

It drives me crazy when I see developers bang on a problem instead of working methodically to understand it. This is what works for me, but maybe there are other methods. How do you approach bug fixes?

Posted on Friday, March 20 2009.
Justin starts a blog

nyc. hacker. blip.tv. vegan. drunkard.

Previous Next