Week 25 · Unit 10: Be a game developer · 3 lessons
1
Run, spot, fix, check
Computing 3: Unit 10, pp. 147–160
GoalFollow the four steps of debugging to find and fix an error in a program.
New words
debug · to find and fix errors in a programbug · an error in a programmatch · when the program does exactly what the algorithm says
Explanation
Debugging means finding and fixing the bugs in a program. We go round four steps. Run: start the program and watch closely. Spot: put the algorithm next to the program and read them line by line, until you find the line where they stop matching. Fix: change that line so that it says what the algorithm says. Check: run the program again. Never skip the last step, because a fix can fail, or it can cause a new bug somewhere else.
run and watch→spot the line that does not match→fix that line→run it again to check
Worked examples
The algorithm says: say “Hi!” for 2 seconds, and then glide to the right. We test the program. The sprite glides first and speaks afterwards. Side by side, the algorithm has the say block before the glide block, but the program has them the other way round. We swap the two blocks and test again.
Algorithm line
Program block
Match?
when the green flag is clicked
when green flag clicked
yes
go to x: −100, y: 0
go to x: (−100) y: (0)
yes
say “Hi!” for 2 seconds
glide (1) secs …
no
glide 1 second to x: 100, y: 0
say [Hi!] for (2) seconds
no
Compare the algorithm and the program line by line. A table helps us to see where they stop matching.
Sometimes there are two bugs. After the first fix, the test still shows a problem, so we go round the steps again. We stop only when the program matches the algorithm in every line.
Class activity
Debug the walk (in pairs): one of you writes a four-step algorithm for a walk round the room, such as “1 Take 3 steps forward. 2 Turn right. 3 Take 2 steps forward. 4 Clap.” On a second card, write the program for it but change two things secretly. The other one acts the program, finds where it stops matching the algorithm, fixes it and acts it again. Did you find both bugs?
Practice
1
Put the debugging steps in order: (a) fix the line (b) run the program and watch (c) spot where the program and the algorithm stop matching (d) run it again to check.
b, c, a, d.
2
The algorithm says: when the space key is pressed, next costume, wait 1 second, next costume. Find the bug in the program.
The wait block says 20 seconds, but the algorithm says 1 second. Change the 20 to 1.
3
Why do we test again after we fix a bug?
To check that the fix works and that it has not caused a new error.
4
The algorithm says: when the green flag is clicked, say “Ready” for 1 second, and then move 50 steps. What is wrong with this program?
The second block is a turn block, so the sprite turns 50 degrees instead of moving. Change it to move (50) steps.