Tutorpip← About this lessonOpen the sheet →Download PDF
Ages 10+ · Coding · Build a project
Reading other people's code
The program• set coins to zero• repeat four times• add three to coins• say coinsWhat it does• the box starts at 0• runs what is inside• the box grows by 3• shows what it holds
A tiny program and what each line does.
The big ideaRead code line by line, use names as clues, and try it with small numbers.

How it works

Read code like a recipe: start at the top and say what each line does, in your own words.

A name in code is a clue, like a label on a box. Good names tell you what each box holds.

Run it by hand with small numbers on paper to see what the code does.

Comments are notes for people, and they can go out of date. Check them against the code, because the code is what runs.

Start atthe topUse thenames ascluesRun it byhandCheck thecommentsTry onechange
How to read someone else's code.

Example 1: Line by line

  1. The tiny program has four lines: set coins to zero, repeat four times, add three to coins, and say coins.
  2. Line one gives the box coins its start value of zero.
  3. Line two repeats what is inside four times, and line three adds three to coins each time round.
  4. Line four shows what the box holds.

Example 2: Run it by hand

  1. Coins starts at 0, and each trip adds 3.
  2. After trip one it holds 3, after trip two it holds 6, and after trip three it holds 9.
  3. After trip four it holds 12, so the program says 12.

Example 3: Names as clues

  1. Mina opens Teo's code and sees beat count, dance speed and moves.
  2. Those names tell her what each box holds before she reads a single line of the loop.

Example 4: A comment goes out of date

  1. A note beside the loop says four moves per song, but the loop makes five.
  2. Running it by hand with beat count 0, 1, 2, 3, 4 and then 5 finds the mismatch.
  3. The code is what runs, so Mina changes the five to a four and tries it.

Words to know

comment
A note in code for people. The computer skips it.
run it by hand
Work through the code on paper with small numbers.
name
A label for a box, which is a clue to what it holds.

Watch out

Try it

  1. A box starts at 0 and 2 is added three times. What does it hold at the end?
  2. Do you read code from the middle or from the top?
  3. A comment says four moves, but the loop makes five. Which do you trust?
  4. Does the computer read a comment and change the answer because of it?
  5. Should you change one thing or five things at a time?
Answers: 1. 6 · 2. From the top · 3. The code · 4. No · 5. One thing
Try at homeTake a recipe or a board game's rules. With a grown-up, read it one line at a time and say what each line does. Which words are names for things? Run it by hand with a small amount, like one egg, and see if the steps give the result you expect.
For grown-upsRead a recipe or a board game's rules together as if it were code: say what each line does, spot the names for things, and run it by hand with a small amount, like one egg.
Open these notes on screenScan the code, or go to tutorpip.com.