The Different Kinds of Bugs


As a general matter, three kinds of errors, or bugs, can occur in a program: syntax errors, runtime errors, and logical errors. We want to understand the kind of bug we have so we can squash that bug!

Syntax Errors

Syntax errors, which are the most easily dealt with kind of bug, are caused by improperly constructed code. Put a little more formally, a syntax error occurs when a statement in a program fails to meet the requirements of the language definition. Therefore, the program can’t be compiled or interpreted—meaning “made ready” for the machine to execute—by the program designed to convert your “high-level” statements to machinereadable code.

Just as there are many ways to sin, there are also many ways to create syntax errors. The most common of these are discussed later in this chapter in the section “ JavaScript Syntax Errors.”

If your code contains a syntax error, it’s likely that you’ll get a syntax error message when you attempt to open it in a browser. I’ll show you how this works later in this chapter in the section “ Finding Bugs.”

The point here is that usually you can’t even get a program to run if it contains syntax errors. This makes things easier because you know you have a problem. As the saying goes, the first step to fixing a problem is knowing you have one.

As I’ll show you, the error message you get in JavaScript with a syntax error will help you to pinpoint the error by providing a line number for the problem. This facility is relatively wimpy compared with what you’ll find in a full-fledged development environment such as Visual Studio .NET, which actually flags syntax errors as you create the statement containing them.

The advantage of learning in a “primitive” language such as the one used in this book (a text editor, JavaScript, and a Web browser) is that you’ll learn to program well without creating too many syntax errors by necessity.

Runtime Errors

Runtime errors occur when a program that has no syntax errors attempts to perform an illegal operation.

JavaScript in a Web browser isn’t a great environment for demonstrating runtime errors because it’s protected by nature and has limited access to system resources.

For example, dividing by zero is generally inadvertent. One scenario that causes a division by zero is if the user inputs a value of zero, which is used in a division. (Of course, it’s far more likely that the zero divisor will be the inadvertent result of a computation.)

In most environments, attempting to divide by zero causes a runtime error. However, in JavaScript, anything divided by zero is the special value Infinity, so attempting to divide by zero doesn’t cause an error (see Listing 10-1 and Figure 10-1).

Listing 10.1: Dividing by Zero

start example
 <HTML> <HEAD> <TITLE>Don't bug me, please!  </TITLE> </HEAD> <BODY> <H1> <SCRIPT>  var theNum = new Number(42 / 0);  var theStr = theNum.toString();  document.write(theStr);  </SCRIPT> </H1> </BODY> </HTML> 
end example

click to expand
Figure 10-1: In JavaScript, anything divided by zero is the special value Infinity.

Trust me, however, when I say that in most environments attempting to divide by zero at runtime will cause an error.

Many runtime errors are caused by attempting to access resources that aren’t available for one reason or another: Because the resource doesn’t exist, the program doesn’t have proper permissions to access it, the resource hasn’t been freed, or the resource is full.

To provide examples of these kinds of errors a little more concretely— they occur when you attempt to write to a full disk, write to a file that doesn’t exist or that’s being used by another program, use a network resource that isn’t available, or log on to a database with incorrect credentials.

There’s nothing syntactically illicit about any of these things; they just can’t be done. They also can’t all be easily anticipated.

The most embarrassing thing about runtime errors is that they often don’t appear when you’re testing a program. Runtime errors generally start to make themselves known only after your program is installed and someone starts using it, but good testing methodology, as I explain later in this chapter, helps obviate this possibility.

The best thing to do is to anticipate possible runtime problems and handle them using exception handling code as explained later in this chapter. If the user might enter zero causing a divide by zero flaw, handle that possibility using a catch clause. If your program depends upon access to a network drive, handle the possibility that the drive isn’t available. And so on.

Logical Errors

Logical errors occur when a program runs properly but produces the wrong results. This kind of error can be the hardest of all errors to debug because, unlike runtime errors, a logical error will often not produce a dramatic or obvious symptom such as a program crash. In contrast, in most environments (but not in JavaScript in a browser) there’s no doubt that a program will crash if you try to perform an unhandled division by zero.

In fact, logical errors can cause inconsistent results that are sometimes correct and sometimes wrong. This situation can be particularly frustrating to debug. The subtler the problem, the harder it may be to fix (or to be sure that an error even exists).

Thinking carefully about how your program works is one of the best ways to resolve logical errors. I’ll show you some techniques later in this chapter for pinpointing logical errors although, frankly, the facilities available to JavaScript programmers working in a text editor are minimal.

As I’ve mentioned a number of times, one of the most common logical errors involves being “one off” when iterating through a loop. One-off errors are very common and quite pesky. You should always be on the lookout for one-off errors. Essentially, this error involves getting the termination condition for the loop wrong—it’s off by one.

It’s also easy to forget that JavaScript arrays start at zero and assume that they start at one.

Another condition that can be the source of problems is getting the termination condition of a while statement wrong.




Learn How to Program Using Any Web Browser
Learn How to Program Using Any Web Browser
ISBN: 1590591135
EAN: 2147483647
Year: 2006
Pages: 115
Authors: Harold Davis

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net