Tracing code line by line: why reading comes before writing

Tracing means running code in your head, one line at a time. Why students who can't trace struggle to write code, and how to explain a trace on video.

By the Chasca team 6 min read

This video was made with the Code problems skill. Read the transcript

Tracing code means following it the way the computer does, one line at a time, writing down each variable’s value whenever it changes. Students who can’t trace reliably struggle to write programs, which is why exam boards test it directly and why some researchers argue it should be taught before writing code.

Why does tracing matter so much?

Because many students finish their first programming course unable to program, and weak tracing is part of the reason. In 2001, a working group led by Michael McCracken gave first-year computer science students at four universities a trial programming assessment: the 216 students averaged 22.89 out of 110.

A follow-up group asked why. Raymond Lister and colleagues tested students from seven countries on reading and tracing short pieces of code: predicting what a short piece of code would produce, and choosing the missing line of nearly finished code. Many were weak at both, which the authors read as a fragile grasp of the skills that problem solving depends on.

That shaped a teaching idea. In a 2019 theory of instruction for introductory programming, Benjamin Xie and colleagues at the University of Washington separate four skills, with tracing first, and argue for teaching reading before writing. In their exploratory study, material that taught the skills in that order led to higher completion of practice exercises, fewer errors and better understanding on a post-test.

How is tracing tested?

Directly. England’s GCSE computer science specifications build it in. AQA’s specification names code tracing among the skills its first paper assesses and expects students to use trace tables to work out how simple algorithms work and what they are for. OCR’s requires students to create and use trace tables to follow an algorithm, and to identify errors in code and suggest fixes. Both also ask for truth tables, and AQA’s includes simple SQL queries.

What does a trace look like?

Take a short question of the kind a GCSE paper asks: what does this program print?

total = 0
for n in range(1, 5):
    total += n
print(total)

A trace follows the computer one change at a time:

Step n total
Start 0
First pass 1 1
Second pass 2 3
Third pass 3 6
Fourth pass 4 10

The idea that matters is that range(1, 5) stops before 5, so the loop runs four times and the program prints 10. A student who answers 15 has run the loop once too often, and a good explanation says so.

Why is tracing hard to learn on your own?

  • Running the code shows the output, not the why. A student sees “10” and still doesn’t know which line made it.
  • A finished trace table shows everything at once, so the order in which values changed, which is the whole point, is lost.
  • Class time is short. A teacher can trace an example or two on the board; the homework sheet has many more.
  • Explanations online cover topics, not the exact snippet a student is stuck on, and rarely in the student’s own language with the code words left as they are.

What does a good tracing video show?

  1. The code, in view the whole time, beside the working, with the line being run marked.
  2. Each value as it changes, said and written one change per line: “i = 2 → total = 3”.
  3. The idea that matters, named: the loop runs four times, integer division drops the remainder, the base case stops the recursion.
  4. For an error, the line, why it fails and the fix. For a number conversion, each division by the base. For SQL, which rows are kept and why.
  5. The output boxed, then a quick check.
  6. Code read as programmers say it, names and keywords in English, even when the explanation is in another language.

Keeping the code and the trace together matters. Paul Ginns’ 2006 meta-analysis of 50 studies found that bringing related information together, in space and in time, gives substantial learning gains, especially with complex material. We cover more of that research in why whiteboard videos work.

What does the Code problems skill do?

We make Chasca, so take our part of this with that in mind. The Code problems skill works computing questions on a teacher’s board:

  • Bring the code and the question, typed or pasted, or as photos, a PDF or a document of the sheet, up to 20 pages: output questions, finding errors, number systems, truth tables and SQL on a small table.
  • Your code sits beside the working from the start, copied exactly, and is walked through as the computer runs it, with the line that matters highlighted.
  • The trace is written on the board one change at a time as it is said. Trace tables, truth tables and query results are drawn as tables, an algorithm given in steps as a flowchart, and recursive calls as a tree.
  • Code words are read the way programmers say them, in English, inside narration in your language, in any of 30 narrators or your own voice.
  • Every answer is solved a second time, separately, before it is boxed. If anything disagrees, or disagrees with your key, the video’s page tells you which question to check.
  • Ask for questions on a topic without giving any, and it writes short exam-style code for them, in Python unless you name another language.

Who is it for?

Computer science teachers who want a worked trace for every question on a sheet, tutors preparing students for school exams, and instructors on introductory programming courses whose students need to read code fluently before they write it.

What won’t it do?

  • Run your code. It is traced the way a teacher would on a board, then checked by solving it again; it isn’t executed on a computer.
  • Draw logic gate diagrams yet. Truth tables, yes; gate symbols, not yet.
  • Teach a whole language. It explains questions, one at a time.

How do you make one?

  1. Open Code problems in Chasca.
  2. Paste the code and the question, with its options and your key if it has them, or add a photo or PDF of the sheet.
  3. Pick the language, the voice and a black board or whiteboard, then make the video.
  4. Check the video’s page for any flagged question before you share it.

Your first two videos are free, up to a minute each, watermarked and with no card required; a free video covers the first questions that fit in its minute. Plans start at $9.99 a month (pricing).

Sources

Video transcript What the narrator says in the 2:25 explainer above.

In this video, we trace a Python list and convert a decimal number to binary and hexadecimal.

Question one. What is the output?

First, x equals a list containing one, two, and three.

Next, y equals x, so both names refer to the same list. The call y dot append with four adds four to that shared list. Now x and y both refer to the list containing one, two, three, and four.

The print call evaluates len of x, which is four.

Notice that assigning y equals x does not copy the list. Hence, the output is four.

Question two. Convert forty-five to binary, then to hexadecimal.

First, repeatedly divide forty-five by two, keeping each remainder. Forty-five divided by two gives twenty-two with remainder one.

Twenty-two divided by two gives eleven with remainder zero.

Eleven divided by two gives five with remainder one.

Five divided by two gives two with remainder one.

Two divided by two gives one with remainder zero.

One divided by two gives zero with remainder one.

Reading the remainders from bottom to top gives one zero one one zero one in binary. For hexadecimal, divide forty-five by sixteen to get two with remainder thirteen.

Then divide two by sixteen to get zero with remainder two.

In hexadecimal, thirteen is written as D. Reading those remainders from bottom to top gives two D. As a check, two times sixteen plus thirteen equals forty-five.

Hence, forty-five is one zero one one zero one in binary and two D in hexadecimal.

Questions people ask

Tracing means following a program line by line the way the computer would, writing down each variable's value whenever it changes, to predict what the program does or prints. A trace table is the usual way to record it.

The evidence leans that way. A 2019 theory of instruction for introductory programming puts tracing first, and in its exploratory study, teaching the skills in that order improved exercise completion and reduced errors.

Give each variable, and the output, its own column. Step through the code line by line and add a row each time a value changes; the last row holds the final values and the output.

Because they have to hold every variable in their head while following the order in which the computer runs the lines. A 2004 study across seven countries found many novice programmers weak at predicting what short pieces of code would produce.

No. The Code problems skill traces the code the way a teacher would on a board, and every answer is solved a second time, separately, before it is boxed. If the two disagree, the video's page tells you which question to check.

Output and tracing questions, finding and fixing errors, number conversions such as denary to binary, truth tables and SQL queries on a small table. It doesn't draw logic gate diagrams yet.

Keep reading

  • 1:45 video A frame from an economics video: a market equilibrium question and its working on a dark board, beside a supply and demand graph where demand shifts out and the equilibrium moves from E1 to E2

    Guides

    Economics graphs explained step by step

    Build the graph in front of the student: axes, each curve as it's named, the equilibrium, then the shift. Where students lose marks and how to explain it.

    5 min read

  • 4:38 video A frame from a grammar lesson for Spanish speakers: "¿Cómo se forma y cuándo se usa el present perfect?" with the rule "have/has + participio pasado", an example and a table of subjects and auxiliaries

    Guides

    Grammar lessons in the learner's own language

    Explain the rule in a language the learner already knows and keep the examples in the one they're learning. What the research says, and how to make one.

    5 min read

Your next explainer takes under a minute.

Your first 2 videos are free, up to 1 minute each, watermarked. No card required. Full product, every voice and style included.