Choosing a Hand Trace or a Debugger

A hand trace focuses attention on a few values. A debugger can inspect the program's actual state where a larger calculation becomes surprising.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Pick a small model
  2. Inspect the live state when needed
  3. Predict a short execution before inspecting it
  4. Observe an input while keeping side effects in mind

Pick a small model

For an off-by-one loop over three items, list the visited indices on paper and compare them with the expected range. Tracing program state can reveal a reasoning mistake before changing the source.

Inspect the live state when needed

A debugger can stop at the first unexpected index and show values inherited from callers. Record those starting values to reproduce the failure from a smaller call. Stepping through calls explains how to find the relevant frame.[1]

Predict a short execution before inspecting it

When a loop over three values returns an unexpected sum, write an expected row for each input and accumulator before running. A breakpoint at the update statement can show the first divergence. A hand trace provides independent expected values for that comparison. See tracing program state.

Input [3,1,4], initial sum 0
After 3 expect 3
After 1 expect 4
After 4 expect 8
Compare each with the live variable

Observe an input while keeping side effects in mind

Inspecting state in a debugger can affect timing and evaluating an expression may execute code. For concurrent or time-sensitive failures, use deterministic scheduling or suitable logs when possible. Keep a minimal failing input for repeatable inspection.

References

  1. Python Software Foundation, pdb: The Python Debugger ↩