Grade by Grade
C. Algebra · Patterns, Equality and Coding

Reading and Changing Code: Fixing Bugs

C3.2About 35 minutes
Learning goals
  • I can read code and tell what will happen.
  • I can find a bug (a mistake) in code.
  • I can change code to fix it or to make something new happen.
  • I can explain how changing the code changes what happens.

Words to know

bug
A mistake in code that makes it do the wrong thing.
debug
To find and fix the bug in code.
alter
To change something.
outcome
What happens at the end; the result.
obstacle
Something in the way that you must go around.

Uh oh, a bug!

Sometimes code does not do what we want. A mistake in code is called a bug. Finding and fixing the bug is called debugging.

Every coder finds bugs. Even grown-up coders who make video games and apps! Debugging is a big part of coding, and it is like solving a puzzle.

How to debug

  1. Read the code, one step at a time.
  2. Run it with your finger or with objects.
  3. Find the step where it goes wrong.
  4. Change that step.
  5. Run it again to check.
Beep starts in the bottom-left square. The star is 4 squares to the right and 2 squares up. The dashed line shows where the code below takes Beep.
START
➡ Move right 3
⬆ Move up 2
STOP

This code has a bug. Beep stops one square to the left of the star.

Worked example

Debug Beep's code

The code is Move right 3, Move up 2. Beep ends one square left of the star. Fix the bug.

  1. Read the code and run it with your finger: right 1, 2, 3. Up 1, 2.
  2. Beep is in the right row, but one square too far left.
  3. So the problem is how far Beep moves right. Count the squares from Beep to under the star: 1, 2, 3, 4.
  4. Change Move right 3 to Move right 4.
  5. Run it again: right 4, up 2. Beep lands on the star!

The fixed code is ➡ Move right 4, ⬆ Move up 2. Changing 3 to 4 made Beep go one more square to the right.

Watch out for obstacles!

Now there is a pond in the way. Beep cannot swim! Look at the grid.

~
Beep starts in the bottom-left square. The pond is in the bottom row, 3rd square. The star is 3 squares to the right and 2 squares up.
START
➡ Move right 3
⬆ Move up 2
STOP

This code would make Beep go into the pond! Splash!

Worked example

Fix it by changing the order

Change the code so Beep goes around the pond and still reaches the star.

  1. Run the code: Beep moves right along the bottom row. Square 1 is fine, but square 2 is the pond. That is the bug.
  2. What if Beep goes up first? Then Beep is no longer in the bottom row.
  3. Swap the order of the two steps: Move up 2 first, then Move right 3.
  4. Run it: up 2 (Beep is now 2 rows higher), right 3 (Beep moves along the row with the star). No pond in that row!

The fixed code is ⬆ Move up 2, ➡ Move right 3. Changing the order of the steps changed Beep's path, so Beep goes around the pond.

START
⬆ Move up 2
➡ Move right 3
STOP ⭐

The fixed code. The steps are the same, but the order is different.

Changing code changes the outcome

We can also change code on purpose, to make something new happen. Watch what happens to the number when we change one step.

CodeRun itOutcome
Start with 10, Add 2, Add 210 → 12 → 1414
Start with 10, Add 5, Add 510 → 15 → 2020
Start with 10, Add 5, Add 5, Add 510 → 15 → 20 → 2525
Start with 20, Add 5, Add 520 → 25 → 3030
Changing the numbers, adding a step, or changing the start all change the outcome.
Worked example

Describe the change

Code A: Start with 10, Add 2, Add 2. Code B: Start with 10, Add 5, Add 5. How did the change affect the outcome?

  1. Run Code A: 10 → 12 → 14.
  2. Run Code B: 10 → 15 → 20.
  3. We changed Add 2 to Add 5. Adding a bigger number each time makes a bigger total.
  4. 20 is 6 more than 14 (each step added 3 more, and there are 2 steps: 3 + 3 = 6).

Changing Add 2 to Add 5 made the outcome bigger: 20 instead of 14.

Worked example

A bug in the order

Here is code for making a snowman. What is the bug?

  1. Put on the hat
  2. Roll a big ball
  3. Roll a middle ball and put it on top
  4. Roll a small ball and put it on top
  1. Read and run each step in your head.
  2. Step 1 puts on the hat, but there is no snowman yet. There is no head to put it on!
  3. The hat must go on after the small ball (the head) is on top.
  4. Move Put on the hat to the end.

The fixed code: 1. Roll a big ball. 2. Roll a middle ball and put it on top. 3. Roll a small ball and put it on top. 4. Put on the hat.

Your turn

1

The star is 2 squares right and 3 squares up from Beep. The code is Move right 2, Move up 2. What is the bug?

2

The code Move right 4 makes Beep go one square past the star. How do you fix it?

3

Code: Start with 0, Add 10, Add 10. What is the outcome? Change it so the outcome is 30.

4

Code: Start with 5, Add 5, Add 5. Change only the start number so the outcome is 20.

5

Code for a sandwich: 1. Eat the sandwich. 2. Put cheese on bread. 3. Put bread on top. What is the bug?

6

Code: Start with 30, Take away 10. Outcome is 20. What happens if you change Take away 10 to Add 10?

Just for fun

A true story: in 1947, people working on a giant computer found a real moth stuck inside it! They taped the moth into their notebook and wrote that they had found an actual bug.

Hands-on activity

Bug hunt

You need: the floor grid from the last lesson (tape, chalk or tiles), a toy, a pillow (the obstacle) and paper.

  1. A grown-up writes code for you to follow, but puts one bug in it on purpose.
  2. Be the robot and follow the code exactly.
  3. Where did you end up? Find the bug!
  4. Change the code by crossing out the wrong step and writing a new one.
  5. Run it again to check.
  6. Now put the pillow in the path. Change the code so the robot goes around it.
  7. Switch: you write code with a sneaky bug for your grown-up to find.

Be a good debugger

When you find a bug, say what went wrong and why your change fixes it. For example: "Beep stopped too early, so I made the number bigger."

More practice: warm-up

1

Read the code: Start with 5, Add 5. What is the outcome?

2

True or false? A bug is a mistake in code.

3

The star is 3 squares to the right of Beep. The code says Move right 2. What happens?

4

Code: Start with 10, Add 10. Change Add 10 to Add 20. What is the new outcome?

5

Code for playing in the snow: 1. Go outside. 2. Put on your boots. What is the bug?

More practice: on your own

1

The star is 4 squares up from Beep. The code says Move up 5. What is the bug? How do you fix it?

2

Code: Start with 0, Add 5, Add 5, Add 5. What is the outcome? Change the code so the outcome is 10.

3

Code: Start with 40, Take away 10, Take away 10. What is the outcome? What happens if you change the start to 50?

4

The star is 3 squares right and 1 square up from Beep. The code is Move right 3, Move down 1. What is the bug?

5

Code: Start with 2, Add 2, Add 2. The outcome is 6. Change the code so it counts by 2s all the way to 10.

Challenge

1

Beep starts in the bottom-left square. A pond is in the bottom row, in the 2nd square. The star is 2 squares right and 1 square up. The code is Move right 2, Move up 1. What goes wrong? Fix it.

2

Code A: Start with 10, Add 10, Add 10. Code B: Start with 20, Add 10. Do they have the same outcome? Explain.

3

This code has two bugs. Beep needs to go 2 squares right and 3 squares up. The code is ⬅ Move left 2, ⬆ Move up 4. Find and fix both bugs.

Check your understanding

Quiz

1.

What is a bug in code?

2.

Beep needs to go 5 squares right. The code says Move right 4. How do you fix it?

3.

Code: Start with 10, Add 10, Add 10. If you take away the last step, what is the new outcome?

4.

A pond is in Beep's path. What could you change in the code?

5.

Code: Start with 15, Add 5. What happens if you change Add 5 to Take away 5?

6.

The star is 2 squares up from Beep. The code says ⬇ Move down 2. What is the bug?

For parents and teachers

Reading and changing ("altering") code teaches children to predict, test and revise, which is the heart of problem solving in math. It also builds a healthy attitude to mistakes: a bug is just something to fix.

Common mistakes

  • Rewriting all the code instead of finding the one wrong step. Encourage running the code step by step and stopping where it goes wrong.
  • Fixing by guessing. Ask your child to say why a change will work before testing it.
  • Not noticing that the order of steps is the bug. Act out the steps physically to make order problems obvious.

How to help

  • Deliberately give silly, buggy instructions ("Put on your shoes, then your socks") and let your child debug them.
  • Praise persistence and explaining: "You found the bug and told me why. That's what real coders do!"
  • Ask: What would happen if we changed this number? What if we added one more step?