Bugs are the bane of the software industry. They can be difficult to find and costly to fix. How we deal with bugs has a big impact on our software development process. We can use bugs to show us ways of improving our process so that similar bugs aren’t created again. Here are seven strategies for fixing bugs.
1. Don’t write them in the first place
You know the old joke: The patient raises his arm and says to his doctor, “Hey Doc, it hurts when I do this,” and the doctor replies, “Well then, don’t do that.” We want to avoid writing bugs in the first place and can do this by focusing on creating high quality code and using our tools to help us prevent mistakes.
2. Catch them as soon as possible
If we’re going to write bugs then we should have a process for finding them as quickly as possible. When time elapses between when a bug is written and when it’s fixed the code containing the bug becomes less familiar to the developer who wrote it and takes longer to fix. But with a set of automated regression tests developers can get instant feedback on whether or not their code has most kinds of bugs. By reducing the cycle time from when a bug is written to when it’s repaired to zero the cost of fixing the bug is the lowest it will ever be.
3. Make bugs findable by design
No matter how good our regression tests are some bugs will get away from us, and when they do it would be good if they were easier to find. Our ability to find bugs in code is directly related to the code’s qualities. For example, software that is highly cohesive and well encapsulated is less likely to have side effects that can cause bugs. Software that is cohesive and well-encapsulated is also easier to read and understand so it’s also easier to find bugs in it.
4. Ask the right questions
Since developers spend most of our time and effort in debugging trying to find bug, figuring out how to quickly locate bugs is important. I had a genetics professor in college, Dr. John Elision, who used to say, “It doesn’t matter if your experiment succeeds or fails, what matters it that you learn something.” This is good advice for debugging too. The rare times that I find myself hunting down a bug I try to construct scenarios that give me good information on where the bug could be or at least where the bug isn’t and I’ll try and narrow down sections of code until I find it.
5. See bugs as missing tests
Once I find a bug and before I fix it I write a failing test for it so that when I fix the bug the test passes. The bug came about because of some false assumption. By figuring out what that false assumption is and embodying it in a unit test we get regression for that issue and will never have to deal with that bug again.
6. Use defects to fix process
When I find a bug I ask why the bug happened in the first place. Sometimes this leads me back to a problem in my software development process and fixing the process can potentially rid me of many future bugs. Look for ways to let our tools help us do the right things.
7. Learn from mistakes
If bugs represent false assumptions or flaws in our development process then it’s not enough to fix bugs, we must fix the environment that allowed the bug to happen in the first place. Use bugs as lessons on vulnerabilities in your design and process so you can look for ways to fix them. Use mistakes as learning opportunities and gain the valuable message each of our problems hold.
Bugs are the single largest expense in software development, costing billions of dollars per year. Bugs aren’t just problems in code they’re also problems in our software development process, and by seeing them as such we can use them to find ways of improving our process thereby eliminating similar bugs in the future.
Previous Post: « Seven Strategies for Burning Down Risk
Next Post: Seven Strategies for Growing as a Software Developer »