Java Reference
In-Depth Information
During debugging, compare the actual contents of variables against the values
you know they should have.
A debugger shows you what the program does. You must know what the program
should do, or you will not be able to find bugs. Before you trace through a loop,
ask yourself how many iterations you expect the program to make. Before you
inspect a variable, ask yourself what yo u expect to see. If you have no clue, set
aside some time and think first. Have a calculator handy to make independent
computations. When you know what the value should be, inspect the variable. This
is the moment of truth. If the program is still on the right track, then that value is
what you expected, and you must look further for the bug. If the value is different,
you may be on to something. Double-check your computation. If you are sure your
value is correct, find out why your program comes up with a different value.
273
274
In many cases, program bugs are the result of simple errors such as loop
termination conditions that are off by one. Quite often, however, programs make
computational errors. Maybe they are supposed to add two numbers, but by
accident the code was written to subtract them. Unlike your calculus instructor,
programs don't make a special effort to ensure that everything is a simple integer
(and neither do real-world problems). You will need to make some calculations
with large integers or nasty floating-point numbers. Sometimes these calculations
can be avoided if you just ask yourself, ȒShould this quantity be positive? Should
it be larger than that value?ȓ Then inspect variables to verify those theories.
Step 5 Look at all details.
When you debug a program, you often have a theory about what the problem is.
Nevertheless, keep an open mind and look around at all details. What strange
messages are displayed? Why does the program take another unexpected action?
These details count. When you run a debugging session, you really are a detective
who needs to look at every clue available.
If you notice another failure on the way to the problem that you are about to pin
down, don't just say, ȒI'll come back to it laterȓ. That very failure may be the
original cause for your current problem. It is better to make a note of the current
problem, fix what you just found, and then return to the original mission.
Search WWH ::




Custom Search