Week 25 · Unit 10: Be a game developer · 3 lessons
1
Hunt the bug, part by part
Computing 4: Unit 10, pp. 138–151
GoalTest a program one group of blocks at a time, record the results in a table and use the table to find and fix a bug.
New words
bug · a mistake in a programdebug · to find a bug and fix itsystematically · one part after another, in order, so that nothing is missedtest table · a table that lists each test, the expected result and the actual result
Explanation
When a program does not do what we want, there is a bug. To debug it, do not guess. Test it systematically. A program is made of several groups of blocks, and we can click on one group at a time to run it alone. Write the result of each group in a test table. Groups that pass are probably fine. Groups that fail point to the bug. If several groups fail and they all call the same sub-routine, look in the sub-routine first. Fix one thing, then test all the groups again, because a fix can sometimes cause a new bug.
read the algorithm→click each group, write the result→find the groups that fail→fix one thing, test again
Worked examples
A rocket game has five groups of blocks. The algorithm says that every time the rumble sub-routine runs, the rocket gets 10 bigger and then 10 smaller, so it ends at the same size. Three of the groups are shown here. They all call rumble.
Group
Expected result
Actual result
Passed?
green flag
rocket at y = −140, size 100
same
yes
space key
y + 25 and size back to 100
y + 25 but size 109
no
l key
y − 25 and size back to 100
y − 25 but size 109
no
click
says Ready!, size back to 100
says Ready!, size 109
no
define rumble
size 110, then size 100
size 110, then size 109
no
The test table for the rocket. Each group was clicked on its own and the results were written down. The group that sets the start position works. The three groups that call rumble all show the same problem, so the bug must be in the part they share.
The bug is in the last block of rumble. It makes the rocket 1 smaller, but it should be 10 smaller. After the fix, every test passes. A rocket that rumbles three times now stays at size 100. Before the fix, it would have grown to 127.
Class activity
Bug detectives (with a friend): one person writes a short algorithm for a sprite, for example “move 10 steps, say Hi for 1 second, turn right 90 degrees, move 10 steps”, and then writes the blocks on paper cards. Before showing the cards, they secretly change one number. The other person acts out the cards and writes down what happens. Where does the actual result differ from the expected result? Find the bug and fix it.
Practice
1
The test table is for a boat game. Which group has the bug? What do you think is wrong with it?
Group
Expected result
Actual result
Passed?
green flag
boat at x = −150
boat at x = −150
yes
left arrow
boat moves left 20
boat moves left 20
yes
right arrow
boat turns right
boat turns left
no
define splash
pop sound, wait
pop sound, wait
yes
The group “right arrow” has the bug, because it is the only one that fails. The boat turns left, so the turn block probably says turn ↺ (left) instead of turn ↻ (right).
2
Three scripts call a sub-routine, and all three show the same problem. The scripts that do not call it all pass. Where do you look for the bug first, and why?
Group
Calls the sub-routine?
Passed?
key a
yes
no
key b
yes
no
click
yes
no
green flag
no
yes
define pulse
(is the sub-routine)
no
In the sub-routine. It is the part that the failing scripts share, and the other scripts, which do not use it, work.
3
Put these steps for debugging in the right order: fix one thing; read the algorithm; find the groups marked no; test all the groups again; click each group and write the result.
1 Read the algorithm. 2 Click each group and write the result. 3 Find the groups marked no. 4 Fix one thing. 5 Test all the groups again.
4
Why should you fix only one thing before you test again?
If you change many things at once, you cannot tell which change helped, and a new change may add a new bug.