Class 6 · Unit 5: Computational thinking · Lesson 5.5 · 40 min
Bug hunters
1947: a computer gives wrong answers. Inside, engineers find a moth. Every mistake since has been called a bug — and finding them is a skill.
Today you will: Hunt bugs in three broken programs · Use predict, run, spot, change one thing · Catch a bug hiding at the edge
Story
In 1947, engineers working on an early computer at Harvard University found that it was giving wrong answers. When they looked inside, they found a moth stuck in one of the parts! They taped it into their notebook with the words "first actual case of bug being found".
People had used the word "bug" for machine faults even before that. But since then, every mistake in a program has been called a bug, and fixing it is called debugging.
Every programmer, including the people who build AI, spends a large part of their time debugging. Making mistakes is normal. Finding them is a skill.
Watch
How to hunt a bug — scan code 6.5.5 in the printed book.
Learn it
Every programmer makes mistakes. The mistakes are called bugs, after a real moth found inside a computer at Harvard in 1947, and finding and fixing them is called debugging. Good programmers are not people who never make bugs; they are people with a calm method for hunting them down. AI engineers debug too: they test a model on new examples, find where it goes wrong, and fix the data or the model.
Words to know
- Bug a mistake in a program that makes it do the wrong thing.
- Debugging finding and fixing bugs.
- Test running a program on purpose to check what it really does.
- Edge case a tricky test value right at the border of a condition, like exactly 6 when the rule says more than 6.
- Bug log a table where you write each bug: what should happen, what happened, what you changed, and whether it is fixed.
Step by step: the bug hunt on a triangle
The program should draw a triangle, but it draws something else:
repeat (3)
move (100)
turn ↻ (90) degrees
- Predict what should happen: a closed triangle, three equal sides.
- Run it.
- Spot the difference: three lines are drawn, but the shape does not close.
- Change one thing: the turn. To get back to the start after 3 turns you must turn all the way round, 360°, so each turn is 360 ÷ 3 = 120°. Change 90 to 120, run again, and the triangle closes. Write it in the bug log.
Why one change at a time? If you change three things and it works, you won't know which one fixed it, or whether another one broke something new.
Test different values, including the edges. A rule "more than 6 is a good apple" must be tested with 3, with 8 and with exactly 6, because that is where bugs hide.
Careful: a bug does not mean you are bad at coding. Professional programmers spend a large part of every day debugging. And change one thing at a time: change three things at once and, even if it works, you will not know which change fixed it.
On the laptop
You need: Scratch Desktop (offline, no account) · a bug log in your notebook or in Calc: Bug · What should happen · What happened · What I changed · Fixed?
Build each program exactly as written — bugs and all. Then predict, run, spot and fix.
Mission 1: Bug 1 — the square that isn't (pairs, 5 min)
Supposed to: draw a square.
when green flag clicked erase all pen down repeat (3) move (100) turn ↻ (90) degrees- ☐1
Predict: what shape will it draw? Then run it. (How many sides does a square have?)
- ☐2
Change one thing and test again. Fill one row of your bug log.
Mission 2: Bug 2 — the cat that never stops (pairs, 5 min)
Supposed to: say "Salaam!" once when clicked.
when this sprite clicked forever say [Salaam!] for (1) seconds wait (1) seconds- ☐3
Run it. Which block keeps going and never ends? Fix it, and log it.
Mission 3: Bug 3 — the apple sorter that sorts wrong (pairs, 8 min)
Supposed to: say "Good apple" if the size is more than 6, otherwise "Small apple".
when green flag clicked set [size] to (8) if <(size) < (6)> then say [Good apple] else say [Small apple]- ☐4
Size is 8. What does it say? What should it say? Fix it.
- ☐5
Test it properly: size 3, size 8, and size 6. What happens at exactly 6 — is that what we want? That's an edge case.
Finished early?
Make your own program with one hidden bug. Swap with a partner and hunt each other's bug.
No laptop today?
The teacher writes the three programs on the board. In pairs, act each one out step by step, find the mistake, write the fix, and fill in the bug log in your notebook.
Debate it
A program that sorts apples for a fruit trader has a bug. For a week, it puts good apples in the "small" box, and the farmer is paid less. Who should fix it, and who should make things right for the farmer? Why is testing a program before people rely on it an act of amanah (trust)?
Check yourself — practice, not a test
1. What is debugging?
- ○ Writing a program for the first time
- ○ Finding and fixing mistakes in a program
- ○ Deleting a program
- ○ Making a program run faster
2. Put the bug hunt steps in order:
- 1. Spot the difference
- 2. Run the program
- 3. Predict what should happen
- 4. Change one thing and test again
3. A square is drawn with repeat (3) [move 100, turn 90]. What is wrong?
- ○ The move should be 90
- ○ It should repeat 4 times, not 3
- ○ The turn should be 100
- ○ Nothing is wrong
4. It is best to change many things at once when debugging, to save time.
- ○ True
- ○ False
5. A tricky test value right at the border of a condition, like exactly 6, is called an ______ case.
Remember
Everyone makes bugs. Good programmers find them: predict, run, spot, change one thing.
Typing practice
Practise accuracy over speed. Today, careful eyes matter more than fast fingers.
- typing.com: any lesson you have already passed, aiming for 95% accuracy or more
- Notice: when you make a typing mistake, you debug it with Backspace.