You know what I'm talking about. You plan the perfect script for hours, check and double-check the documentation, enter the code in carefully bracketed <SCRIPT> tags, recheck your brilliant logic, save out the HTML file and load it into your browser, prepare to be dazzled, and...your ungrateful browser comes back:
. . . . . . . . . . . ^
If you've programmed in C, Basic, or another scripting language, you're likely to sigh and dig in. Here we go again. If you're new to programming, however, these error messages can be frustrating and discouraging. This article gives you an overview of the debugging process and some tips that will serve you well here, as well as elsewhere in your programming life.
What is a Bug?
A bug is an instruction in a program that is causing an undesirable result. There are two main types of bugs: Syntax and logic errors. Syntax errors happen when you say something the computer doesn't understand, causing program interpretation to stop. Logic errors are when the program works, but doesn't do what you thought you meant.
A syntax error is asking for a "scwerdriver".
A logic error is asking for a flathead screwdriver, then trying to use it on a Philips screw.
You can follow along with the bugs.html file, which gives examples of many of the problems I talk about here.
When the browser gives you a syntax error, note the line number of the error. When you dig back into the script to see what's wrong, however, check for problems above and around that line for the problem as well. An error above the line, especially a missing ), >, or } could be causing the browser to misinterpret what happens next.
On a related note, be aware of "cascading" errors. This means that one error, such as an incorrectly defined function, can cause errors further down the line, for example, when you try to call the function. Correct the first error, then try to run the program again. Subsequent errors may be gone.
Here are some more tips on avoiding--and troubleshooting--programming errors.
- Make sure your double and single quotation marks are matched correctly, especially in long expressions.
- Take small steps. Test your program each time you make a small addition or modification. Back up working versions of a script, so that if difficult errors creep in you can easily go back a step.
- If the browser doesn't seem to be responding to your changes, make sure your files are in sync. Did you save a backup to a different folder, then make changes and forget to save the new version out to the original name? I hate it when that happens.
- Finally, add debugging statements. These are statements that tell you what is happening inside the program, what the variables are at different times, and so on. They can be invaluably helpful. Note which lines are debugging statements so you can find and remove them easily when the program works.