Next: None.
Up: Testing and debugging.
Previous: Large-scale testing.
Debugging
Hand traces
Tracing through the code by hand, to see how variables change, is
extremely common - much more common that you might initially think.
It's just much easier to trace through the code than to repeatedly
recompile and run a test case.
Print statements
Adding print statements is another useful technique. You may think
that it's antiquated, but it's in wide use and will continue to do so.
It's just so simple.
Some useful tips for deciding where to put your print statements:
- Print at the top of relevant methods.
This is to get an idea of which methods are actually getting called.
You may often find that something is getting called unexpectedly, or
that something that you expected to be called is not.
- Print relevant variables after each change. If a
variable seems to be getting a bad value, I would print it out after
each time the variable changes. This is particularly significant with
instance variables (and even more so with class variables), as the
sequence in which their value changes tends to be circuituous.
- in front of code that you think is wrong.
If you have some code, and you think something is going wrong in there,
it's often worthwhile adding surrounding code and verifying that
the code is indeed going awry. Often enough, you'll be wrong about where
the problem is, and all the time you would have spent search would be
wasted.
Debuggers
There is another tool called a debugger.
I don't want to overemphasize its usefulness - I use a debugger far less
frequently than I use hand traces and print statements. But a debugger
is still often useful.
Good debuggers have at least the following two features.
- Go through code statement by statement.
This helps trace the program's behavior better, one step at a time.
You have to issue a command to tell it to go onto the next
statement, and you issue this command over and over.
The debugger also allows you to look at the call stack
and to observe any variables' values.
- Add breakpoints into the program. A
breakpoint is a line of source code designated
by the programmer. When you run the program using the debugger, the
debugger will pause the program each time it reaches a breakpoint,
allowing the programmer to take control and observe the call stack,
variable values, and try tracing through the code.
Forte has a debugger built into it. I'll demonstrate it in class.
Next: None.
Up: Testing and debugging.
Previous: Large-scale testing.