Debugging is a science. You make a hypothesis based on your previous experiments, then make predictions either for or against the hypothesis. Next you run the program, providing it with new and hypothetical input; then you observe its output and then either confirm or refute your original hypothesis. What follows is a short list of steps to follow to solve the bugs you invariably create in the process of program writing.
Note |
The real first step is to not write buggy code in the first place. This may seem like a no-brainer, but is important to consider. When a good chess player plays a game he is constantly thinking at least five moves ahead. With 16 different pieces, that could be more than a million different moves. Of course, the chess player doesn't need to analyze every possible move because he knows that many of them would be bad. Experienced players no longer seem to even see the possible bad moves. The same principal applies to program writing. As you become more experienced, you will make fewer and fewer mistakes that result in bugs. Eventually, you will no longer even think about the common mistakes that beginning programmers make—it will become second nature to write non-buggy code. |
The first thing to do when you do encounter a bug is to find out as much as you can about it. For this, the JavaScript interpreter is your most useful resource. Read the error message carefully; it will usually tell you exactly what the problem is. If you don't understand the error message, your second best resource is the Internet. Unlike in grade school, looking up an answer is not cheating in the computer industry.
After finding out as much as you can about the error, the next logical step is to fix it in your source code. If it is a simple bug, by all means dive right in. If you find yourself in the clutches of a beastly bug, you might think about backing up your source file before making drastic changes to it.
The last step in debugging is to test your changes. If everything works correctly, then great, you're done. If not, jump back up to step one and start again. You can't fix a program unless you understand how it works in the first place. The JavaScript interpreter will help you collect data, but it can't interpret it for you, and it certainly can't fix the problem. Only you can do that.