![]() |
![]() |
|
|||||||||||
|
|
|||||||||||||
References:
The most frustrating problem that students run into with any programming language is finding errors in their programs. Most languages, including javaScript, have very carefully defined ways of doing things and any small mistake will cause the program to fail. An analogy would be a word processing program like Microsoft Word. Think how hard it would be if Word refused to print out your paper if there was any grammatical error. If you forgot a period, used a wrong tense, didn't indent correctly, or made any other mistake, you wouldn't be able to print. This is what programming is like.
There are actually two categories of common errors in computer programming, syntax errors and logic errors. Syntax errors are mistakes in grammar. These include spelling errors, punctuation errors, and other mechanical errors. Logic errors are mistakes in the way that you solved the problem. To go back to the Word analogy, a syntax error would be something like a misspelling or forgetting a period at the end of a sentence. A logic error would be making a false statement in the paper. The statement would be formed correctly and would read fine. It would just be false.
Many programming languages are compiled which means that you have to run a special program to translate your program into a form that the computer can understand. This has advantages and disadvantages, but a strength of this approach is that the compiler will point out your syntax errors to you. JavaScript is an interpreted language which means that it does not have to be translated into a machine language. Instead, an interpreter (which most browsers contain) will read your javaScript program and execute it directly.
The disadvantage to interpreters is that the interpreter won't look at your entire program at once and point out all the errors for you like a compiler will. Instead, it will look at your program one line at a time. If a line contains an error, it will stop. This can be much harder to debug.
In this class we will be building many javaScript programs and you will, as a consequence, be debugging many program. Debugging is a skill, but it can be very difficult in interpreted languages like javaScript. In the remainder of this web page I'll talk a little bit about how you can track down errors in your javaScript programs.
Debugging JavaScript
The following steps provide one method for finding errors and debugging javaScript programs. As you become more skilled in javaScript programming, you'll develop your own techniques.
For example, one common method of writing a program is to start with a program that does something similar. Incrementally change lines in the old program, testing after every change. It is crucial to test after every small change because this isolates possible errors and makes them easier to identify and fix.
One way to do this is to get a print-out of your program and pretend that you're the computer. Go through the program line by line and execute each line in your mind. Draw boxes on a piece of paper for variables and change the values in the boxes as the lines of code change them. Though seemingly crude, this manual technique is often the best way to find logic errors in your program.
Many programming languages have special programs called debuggers that will automate this tracing process. These programs allow you to run your program and step through it one line at a time. At each line, you can examine the values in variables. Browsers don't have this ability, unfortunately, so you will have to mimic this process by explicitly printing out values at various lines using statements line document.writeln(varName). Put these write statements at the beginning of functions, in if statements, inside loops. Write out the values of any variables that change (including loop variables). The more values that you write out, the more information you'll have about the program.
Don't just write out variable information. Be sure to indicate where the line was in the program. For example, printing "inside the first while loop, x has value 5" tells you exactly where you're executing. This is especially important when a variable's value changes at many different places in the code.
In summary, to debug a program, you have to think like a computer. You have to learn to recognize very minor syntax errors and learn how to think sequentially. Debugging a program is never easy and can be the most frustrating experience of your life. But as you become more proficient, many of these skills will become second nature and you too will become a "code wizard".
Return to John Barr's Home Page
|
Last
updated on September 15, 2002 by
John
Barr
|