Week 13 · 13.2 · Tue, Oct 27 · I Do reading
Tracing Code by Hand
1 This section explains why people who read code keep track of its values on paper, and how a trace table does that work.
What the Class Has Practiced
2 Yesterday, the class learned that a program stores values in boxes. Each store step works out its right side and then replaces the old value in the box on the left. On the Store It bench, every snippet was two to five lines long and used one to three boxes. Snippets that short can be followed without writing anything down, and many students did exactly that.
A Small Space for Remembering
3 Psychologists use the term working memory for the small amount of information a person can hold in mind while using it. In 1956, the psychologist George Miller published a well-known paper arguing that people can hold about seven items, plus or minus two, in immediate memory. In 2001, Nelson Cowan reviewed many later studies and concluded that the limit is smaller, about four chunks, when people cannot repeat the items to themselves or group them. A chunk is a group of items that the mind treats as one unit, such as a familiar word instead of its separate letters.
4 Tracing code in the head uses this small space. A person following a snippet must remember which line comes next, the current value of every box, and the result of the step being worked. With two boxes and three lines, that fits, which is why short snippets feel easy. With four boxes that each change several times, the list is longer than working memory can hold, and old values begin to blur with new ones. The mistakes that follow are predictable. A value that was replaced gets used again, or a box that never changed gets a new value by accident.
The Trace Table
5 A trace table moves that work out of memory and onto paper. It is a grid with one column for each box and one row for each step of the code. Each row records one line that runs, and the values in that row are what every box holds right after the line runs. Because each row stays on the page, the person tracing can look up any value instead of remembering it, and a partner or teacher can check the work one row at a time. Teachers sometimes call this "running the code with your fingers," because one finger moves down the code one line at a time while the pencil records what the computer would store.
6 The table starts with its header row, and building the header is part of the method. Before tracing any line, the reader scans the code for every name that appears on the left of an arrow and gives each one a column. A column for the line number goes on the left, and a column for anything the code shows goes on the right. A name that is stored many times still gets only one column, because it is one box. Building the header shows how much there is to keep track of before the work begins. Figure 1 shows a finished trace table for four lines of code, with its header row at the top and one row below it for each line that ran.
Why the Habit Matters
7 Researchers who have studied the scratch paper of beginning programmers report that students who wrote out the values of variables as they traced tended to answer tracing questions correctly more often than students who wrote nothing. With the table doing the remembering, the reader can put full attention on working out each step. Tracing is also a skill on the AP Computer Science A exam, where many questions show a code segment and ask what it prints or what a variable holds, and students answer without running the code.
8 The snippets will not stay short. Soon the class will meet loops, in which the same lines run again and again. A trace table then needs one row for every line that runs, not one row for every line that is written, so a loop of three lines that runs four times fills twelve rows. A reader who builds the habit on short snippets will be ready when the snippets get long.
Summary
9 Working memory holds only a few items at once, so tracing long code in the head leads to mistakes. A trace table gives each box a column and each line that runs a row, which turns memory work into reading work.
Sources: Miller, Psychological Review (1956); Cowan, Behavioral and Brain Sciences (2001); Lister and others, ITiCSE Working Group Reports (2004); Cunningham and others, ICER (2017); College Board, AP Computer Science A Course and Exam Description (2025); Store It reading, class site (2026).