TopInterfacesDebugging Tips

Debugging Tips

As your labs become more complicated and your knowledge of the syntax of Java grows, you will discover that the type of errors you spend most of your time dealing with will change. The problems caused by errors involving the rules of Java grammar will decrease and the problems caused by subtle logical difference between what you told the computer to do and what you meant to tell it will increase.

The process of removing such logical errors is called debugging. In recognition of its growing importance we want to give you four tips on how to approach it. The first two are ways to avoid complicated debugging problems in the first place, the third is a deep, guiding principle that us useful in debugging, and the last is a simple but very handy practical technique.

  1. Plan ahead.

    Having a good understanding of the problem you are trying to solve is important to successfully debug your program. We have been encouraging you to prepare for labs by outlining each different class in the code. In addition to making your lab time more productive, this practice forces you to think clearly about the problem we are asking you to solve and to explore some of the issues and potential pitfalls that will come up when you sit down in lab to write the program.

  2. Write and test your programs incrementally.

    Compile and test your code after adding small parts. Never wait until you have typed in several long methods before you start testing. This will make finding syntax errors easier, and often can give clues to potential logical errors while the problem is still reasonably small. If you do this often, you will know what you have changed recently, which helps make the next item easier.

  3. Localize the problem.

    The minute your program misfunctions, you will probably have some idea what is wrong. However, there may be a large number of places where things could go wrong. As long as there are so many possibilities, the debugger will tend, while looking at the code related to one possibility, to overlook the error and assume on of the other possibilities must be at fault. Narrowing things down to a single possibility is generally the best way to force oneself to admit a misunderstanding.

    This brings us to the simple technique...

  4. The use of "System.out.println"

    How can we narrow the search for the error down from several possibilities to just one? The answer is to try to rule out one possibility at a time. By strategically placing System.out.println()'s throughout the suspect code.

    The syntax of the command used to display such text is:

        System.out.println("Print this line of text");
    

    Be prepared to use this trick while working on upcoming labs. It can be quite helpful.


TopInterfacesDebugging Tips