Tracing Program State by Hand

A trace records the values that change as a program runs. Follow a few assignments to find the first step where the actual result differs from your expectation.

Source
Anbloq
Language
English
Created
Updated
Chapters
5
Reading time
2 min
In this article5 chapters
  1. What to record
  2. Find the old value
  3. Keep names and objects in separate columns
  4. Trace backward from a failed expectation
  5. Narrow a longer trace

What to record

Give each step a row and each relevant name a column. Execute one statement at a time using the values in the preceding row. At a decision, write down which branch ran. If two names refer to the same mutable object, record that object once and draw both references to it. The rules for name binding tell you which values are available at each step.

Find the old value

Start with boxes = 3 and items = 8. After items = items + boxes, there are eleven items. After boxes = boxes + 1, there are four boxes and still eleven items. A predicted result of twelve assumes the increment happened first. Use the same method for an accumulating loop, keeping one row per iteration until the first disagreement.[1][2]

Keep names and objects in separate columns

Trace this example before running it. Give the first list the identifier A and the second the identifier B. Record what each name refers to after every line. The important distinction is that appending changes a list, while assigning to pending changes which list that name refers to.

pending = ["draft"]
snapshot = pending
pending.append("checked")
pending = ["published"]
print(pending, snapshot)

After the second line, both names refer to A. Appending makes A contain draft and checked. The next assignment creates B with published as its only element and makes pending refer to B. The two values printed are ["published"] and ["draft", "checked"]. If you wrote down draft alone for the second output, the first incorrect row is the append. At that point the two names still refer to the same object.

Trace backward from a failed expectation

Suppose the specification says the snapshot should show exactly what was pending when it was taken. Add an expected column for that promise. After the append, your expected snapshot still contains draft, and the actual object has another entry. A copy at the snapshot line fixes this particular example because the entries are immutable strings. With nested lists, decide which deeper objects must be copied under the shallow copy rules. Then run the trace with two equal lists created independently. Their contents can match even though the alias relationships differ.

Narrow a longer trace

In a 50-line routine, begin with the value that first violates a promised property. Record the assignment or call that produced it and the inputs used there. For a loop, record what should remain true after each iteration, such as “the count equals the number of accepted rows read so far.” When a test fails, keep the smallest input that reaches the first incorrect state. That becomes a regression test even after the implementation changes.

References

  1. Python Software Foundation, Python Language Reference: Execution Model ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩