Turtle Makes the Wiser

Just about everyone has heard the tale of the tortoise and the hare, and the lesson to be learned from it. Many parallels exist between this timeless fable and the debugging process. When trying to solve what appears to be an unsolvable bug, it is hard to resist the temptation to quickly and randomly make changes with the hope that you will stumble upon the solution by accident.

Although this method occasionally works, it has a few drawbacks. First, swapping code and fiddling with application settings without any rhyme or reason is a colossal waste of time when you consider the odds against hitting the magic combination. Second, even if you do strike gold and fix the problem, it is unlikely that you will understand or remember how you did it. In your moment of triumph, this might not concern you much. However, most types of bugs can be tucked away for only so long before they rear their ugly heads again. And when they do, you’ll be right back at square one when it comes to fixing them.

Plan Carefully

When you encounter a bug that looks like it is going to take more than a trivial amount of time to solve, you must make a plan of attack. Take a step back and ask yourself,“What is the most likely cause of the bug?” Jot down some ideas, as well as things you could add or change to test your theories. From these ideas, formulate a set of logical steps to find the bug. All the concepts already introduced in this chapter can be used to aid you in this effort.

Proceed with Caution

Armed with a debugging plan, you can now begin making modifications to both code and application settings. Be careful to make only one modification at a time. That way, you will know what worked if the bug suddenly disappears (which has happened to us on several occasions). Always document the modifications that you make either in the code, in a separate file, or even on a sheet of paper. That way, you can always take a few steps back before proceeding down a different route without having to reload your code from your backup files (you did create backup files before you started, didn’t you?).

The key to the whole process is patience. The most complex bugs usually require a combination of changes to fix them, and there is always an element of luck involved with choosing the right debugging route first. It’s definitely not all in the cards, however, because your luck will most definitely improve with experience. (If only our luck with poker could follow this same trend.) The best part is that if you stick to your debugging plan, you will have a road map to fix the problem the next time it occurs. If you get to the end of your plan before you resolve the bug, don’t give up. Take another step back and follow the strategies outlined in this chapter again. Perhaps you are not looking in the right place after all.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset