6.14. Tracing

Trace prints must be controllable [Trace control: 1-5 points]

A trace print is a statement or sequence of statements that are present for debugging. Such prints can be left in the program, but it must be easy to turn them on or off. Write
  if(trace > 0)
where trace is an integer global variable that indicates the level of tracing, and 0 might be replaced by 1, 2, etc.

Do not comment out traces [Trace comment: 1-2 points]

A trace print should be left in the program but made controllable. It should not be commented out.

Write traces to standard output [Trace file: 1 pt]

For this class, write all trace prints to the standard output. (Larger programs often have provisions to write traces to files.)

What to show in a trace print [Trace info: 1-4 pts]

Every trace print should show:
  1. The name of the function that contains the print;
  2. An indication of where the trace occurs;
  3. An indication of what the values being shown represent, if any values are shown;
  4. Values being shown, if any;
  5. A newline, if appropriate, so that trace prints are not run together on the same line.
For example, a trace print that occurs at the beginning of a loop body in function getCompatibility might look like the following.
  if(trace > 0)
    printf("getCompatibility: [top of loop] working on k = %i\n", k);

Omitting required traces [Trace omit: 1-10 pts]

If the assignment requires traces, then you must include them. Don't add them after everything is working! The point of tracing is to help get things working.

Keep trace code short [Trace big: 1-2 pts]

Do not clutter a function definition with a long sequence of lines that are concerned with tracing. Instead, create a function and call the function to shorten it. Any sequence of more than 3 lines (excluding the controlling if-statements and its associated braces) is too long. Do not write a loop in a trace-print. Use a function instead.