GoalPredict the result of a program by hand, test it, compare the two and find and fix the bug.
New words
predict · to work out by hand what a program will show before it runsdebug · to find and fix a bug in a programactual result · what the program really shows when it runsfix · a change that removes a bug
Explanation
Programs seldom work the first time, so we follow three steps. First, predict. Choose some test data and work out by hand what the program should show, and write it in a table. Second, test. Run the program, or trace it carefully, and write the actual result next to the prediction. Third, debug. If the two are different, look for the cause. Check the order of the blocks, the operators, the numbers, the variable names and the conditions. Change one thing at a time, and then run all the tests again, because a fix can break something else. Never skip the prediction. Without it, we cannot tell whether an answer is right.
predict the result by hand→run the program and write the actual result→compare the two→find the bug and fix one thing→run all the tests again
Worked examples
The change-giver program takes a price. If the price is 20 or less it shows the change from a 20 note, if not it shows no. The price is 13. The prediction is 20 − 13 = 7, and the program shows 7.
price
Predicted
Actual
Result
13
7
−7
FAIL
20
0
0
pass
25
no
no
pass
A bug: the programmer wrote price − 20 instead of 20 − price. The prediction and the actual result are different for the price 13, so the test finds the bug. The test with the price 20 passes, because price − 20 and 20 − price are both 0 when the price is 20. This is why we use several values.
Symptom
Likely cause
the board shows 0 when it should not
a show block comes before the set blocks
the loop never stops
nothing in the loop changes its variable
only the edge value fails
> used instead of ≥ (or < instead of ≤)
the answer is the wrong size
the wrong operator, such as + instead of ×
Clues help us to find a bug. Match the symptom to the likely cause, and then check the blocks.
Class activity
Bug swap (no computer): each player writes a short program of 4 or 5 blocks on paper, and a prediction table for three inputs. Then the player secretly changes one thing to add a bug, such as an operator, a number or the order of two blocks. The partner traces the program for the three inputs and compares it with the table. Where do the results differ? Find the bug and fix it. How many tests did it take to find it? Swap roles. If you have a computer or tablet, build both versions in a micro:bit simulator and compare the board.
Practice
1
Predict what the board shows when pocket is 15, 12 and 9.
pocket = 15: 15 ≥ 12 is true, so it shows 15 − 12 = 3. pocket = 12: it shows 12 − 12 = 0. pocket = 9: 9 ≥ 12 is false, so the else path runs and it shows no.
2
This program should show the average of 8 and 4, which is 6. It shows 10. Find the bug and fix it.
The calculation is b ÷ 2 and then a is added: 8 + 2 = 10. The brackets are in the wrong place. The correct blocks are ((a) + (b)) ÷ (2), which gives 12 ÷ 2 = 6.
3
A lift takes up to 6 people. Which test fails, and what is the bug?
people
Expected
Actual
3
yes
yes
6
yes
no
7
no
no
The test with 6 people fails. The rule allows 6, so the expected result is yes, but the program shows no. The condition uses < 6, and it should be ≤ 6.
4
Put these steps in the correct order: fix the bug, predict the results, find the cause of the bug, run the program, compare the results.
1 Predict the results. 2 Run the program. 3 Compare the results. 4 Find the cause of the bug. 5 Fix the bug (and then run all the tests again).