1.11 Avoiding Errors and Debugging

As you may have guessed by now, writing a perfect program on the first try is an extremely rare occurrence, even for the most experienced of programmers. On the other hand, many common errors (or 'bugs', as they are called) can be avoided or at least detected and corrected quickly if we follow good programming practices. To help you develop good habits in your programming, sections like this one containing suggestions for avoiding errors and debugging (eliminating errors that do occur), appear at the end of most chapters.

Avoiding Errors

The style in which a program is written can have a great deal of influence on the chances that the program will be correct. Here are some ideas that should help to keep you out of trouble.
  1. Use meaningful identifiers. Any identifiers that you use for naming classes, variables, or constants should be chosen so that the purpose of each item is clear to any reader, not just you.
  2. Indent your statements. Note and follow the style used in the text or a style suggested by your instructor. In any case, be clear and be consistent.
  3. Use comments to explain your code. A short comment just before a section of code will be a helpful reminder later on of the purpose of that section of code.
  4. Avoid cluttering up your program with too many comments. It is necessary nor desirable to comment every line of your program. Comments are supposed to assist readers in understanding a program's actions, not to distract them.
  5. Use blank lines to separate sections of a program. If logical units of a program are visually isolated from each other, a reader can see the structure and absorb its meaning a chunk at a time.
  6. Use named constants (declared with the final attribute) rather than literals for most constant values in your programs. They can make programs more readable, more reliable, and easier to modify.

Debugging

The errors that inevitably arise in programming can be put into a variety of categories. Two such categories are syntax errors and logical errors. A syntax error in English is a mistake in the grammar rules of the language. Similarly, a syntax error in Java is a mistake in following the rules of that language. A syntax error will be detected by the Java compiler when it attempts to produce byte code. Syntax errors are, for this reason, sometimes called compile-time errors. A program that contains a syntax error will not be compiled. With a logical error, on the other hand, the program can be compiled but, when we attempt to run the program, it does not perform its intended task.

In this section, we will be looking primarily at ways of dealing with syntax errors but the elimination of such errors is only the first step in debugging. Once the program is free of syntax errors and executing, check it for logical errors by examining the output to see if, in fact, the program is doing what you want it to do. We will be examining logical errors more closely in later chapters. For now, here are some suggestions for handling errors that appear during compilation.
  1. If a program fails to compile, start your correction process by reading the first error message produced by the compiler. A single error can cause the production of many error messages with later ones being written because the original error confused the compiler.
  2. It is extremely difficult to have the compiler produce the "correct" message for every possible error condition, but usually the message will be reasonably informative. Read the message very carefully to see what the compiler is trying to tell you. Once you have corrected an error, try to memorize the message that it produced. The next time that you see that message, you should be able to correct the error more quickly.
  3. A common slip is the failure to put a semi-colon at the end of a statement. Since Java's compiler ignores a line break after a statement, it will not detect this error until it reaches the next line. The error message will then be associated with that line. If you get an error message that directs you to one location and you cannot find anything wrong on that line, look at the previous line to see if something there is the cause of your problem.
  4. Compare your program with examples of similar programs that are correct. Look at the syntax that your program uses and compare this with the code of the working program.

Exercises 1.11

  1. Find any syntax errors or logical errors in each of the following English sentences.
    1. My younger sister was born two years before I were.
    2. The comma in this sentence is in the wrong place.
    3. This last parts is. easy
  2. Rewrite the following program using meaningful identifiers, indentation, comments, and blank space to make the program easier to understand.
    class A{public static void main(String[]args){
    System.out.println("In what year were you born?");	
    int a = In.getlnt();
    System.out.println(IIWhat is your name?");
    String b = In.getString();
    System.out.println("Let me confirm that information. II);
    System.out.println(IIYour name is "+b
    +" and you were born in "+a+".");}}
    
  3. The following program contains five known syntax errors. Find them and rewrite the program with the errors corrected.
    class BadNews
    /* a really terrible mess
    {
    public static void main (string[] args)
    	{
    	int i = 34.0, j = 2;
    	System.out.println('Values are i);
    	System.out.println(j);
    	}
    }
    
  4. To gain more familiarity with error messages that will be produced by the Java compiler, take a correct program that you have written and then introduce each of the following errors in that program (one at a time), noting any error message(s) produced in each case.
    1. missing the declaration of a variable
    2. attempting to print the value of a variable that has been declared but not initialized
    3. attempting to change the value of an identifier that has been declared with the final attribute
    4. using a reserved word as a variable identifier
    5. attempting to make a narrowing conversion without the use of a cast
    6. omitting the */ required to close a comment that begins with /*
    7. omitting the first semi-colon in the program
    8. omitting an opening brace bracket
    9. omitting a closing brace bracket