Byte by Byte › Book 4: Office Apps and First Programs › Chapter 11: Finding and Fixing Bugs
Chapter 11: Finding and Fixing Bugs
You will learn: what bugs are, common kinds of bugs in Scratch, a step-by-step debugging method (reproduce, isolate, hypothesise, fix, test), useful debugging tricks, and how to test a program properly.
🌟 The Adventure
The night before the festival, Memoon ran the whole show one last time. It was a disaster.
The dancer entered, then glided straight off the top of the stage. The lanterns lit up before night fell. The star game started, but the stars fell sideways. And the finale flower drew only one lonely petal before stopping.
Noodles hung his head. While Memoon had been at dinner, he had walked across the keyboard, sat on the mouse, and, by the look of it, played with the code.
"I'm sorry," he mewed. "I made it all wrong."
Memoon took a slow breath and checked her watch. Two hours left. She opened her notebook to a clean page and wrote one word at the top: DEBUG.
"Programmers make mistakes all the time, Noodles," she said. "Even without cats. The trick isn't never making mistakes. It's knowing how to hunt them down."

💡 Learn With Memoon
What is a bug? A bug is a mistake in a program that makes it behave differently from what was intended. Debugging means finding and fixing bugs. The words became famous in 1947, when computer scientist Grace Hopper's team found a real moth trapped in a relay of the Harvard Mark II computer and taped it into their logbook as the "first actual case of bug being found". Engineers had already used bug for faults, but the moth made the story legendary.
Every programmer writes bugs. Good programmers are good at debugging.
Common kinds of bugs in Scratch.
| Kind of bug | What goes wrong | Example |
|---|---|---|
| Sequence bug | Blocks in the wrong order | show placed after glide, so the sprite glides invisibly |
| Wrong value | A number or choice is wrong | point in direction 0 (up) instead of 90 (right) |
| Missing initialisation | Starting position or state not set | A sprite starts wherever it ended last time |
| Loop bug | Wrong count, wrong block inside or outside the loop | A turn placed outside the loop, so the flower draws one petal |
| Condition bug | Wrong comparison, missing if checked only once instead of inside forever, and versus or mixed up | forever |
| Event or message bug | Wrong hat block, message name, or broadcast missing | Lanterns use when green flag clicked instead of when backdrop switches to night |
| Wrong sprite | Code put on the wrong sprite | The star's falling script on the catcher |
| Timing bug | Things happen too fast to see, or in the wrong order | No wait inside an animation loop |
The debugging method. Detectives don't guess wildly; they follow clues. So do programmers.
- Reproduce: run the program and make the bug happen again. Note exactly what happens and what should happen.
- Isolate: narrow it down. Which sprite? Which script? Which block? Test one script at a time by clicking it directly.
- Hypothesise: make a guess about the cause: I think the turn is outside the loop.
- Fix: change one thing at a time. If you change five things at once, you won't know which one mattered.
- Test: run it again. Did it fix the bug? Did it break anything else? If not fixed, undo and try your next hypothesis.
Debugging tricks.
- Click a script to run just that script, or a single block to test it alone.
- Add say blocks to show what's happening inside: say (x position) or say Reached the loop! This is like writing notes to yourself; programmers call it print debugging or tracing.
- Slow it down: add wait 1 seconds between steps so you can watch each one.
- Tick the checkbox next to a reporter such as x position or direction to show its live value on the stage.
- Rubber duck debugging: explain your code out loud, block by block, to someone (or a toy duck). Hearing yourself explain it often reveals the mistake.
- Compare with your plan: check the script against your pseudocode or flowchart.
- Save versions: before big changes, File → Save to your computer with a new name (show v2), so you can go back.
Testing. A program isn't finished when it works once. Testing means running it in different situations to find bugs before your users do. Test:
- Normal cases: what usually happens (the star is caught).
- Edge cases: the limits (the catcher at the very edge of the stage, a quiz answer of 0, clicking very fast).
- Unexpected input: what a real user might do (typing six instead of 6, pressing a key nobody planned for).
Ask someone else to try it; they will find things you never thought of.

✍️ Try It Yourself
Activity 1: Bug hunt. Each script below has one bug. Build it in Scratch, run it, follow the five-step method, then fix it.
Bug 1 (dancer should enter from the left and glide to centre):
when green flag clicked glide 2 secs to x: 0 y: -100 go to x: -200 y: -100 show
Bug 2 (should draw a hexagon):
when green flag clicked
erase all
pen down
repeat 6
move 80 steps
turn ↻ 60 degrees
Bug 3 (star should go back to the top whenever it touches the catcher):
when green flag clicked
if ⟨touching Catcher?⟩ then
go to x: (pick random -220 to 220) y: 180
forever
change y by -5
Bug 4 (lantern should appear only at night):
when green flag clicked
show
forever
next costume
wait 0.5 seconds
Bug 5 (quiz should say Correct for 8):
ask (What is 4 + 4?) and wait
if ⟨(answer) > 8⟩ then
say Correct!
else
say Try again.
Activity 2: Say-block detective. In your star game, add say (y position) inside the star's forever loop. Watch the numbers fall. At what y value does the star restart? Remove the say block afterwards.
Activity 3: Test plan. Write a test plan for your star game in a table in Word: Test, What I did, What should happen, What happened, Pass/Fail. Include at least two normal cases and two edge cases. Then run your tests.
Unplugged: Buggy directions. Write directions from your bedroom to the kitchen with one deliberate bug (a wrong turn or a missing step). Give them to a family member to follow exactly. Can they find the bug? Use the five steps.
🎉 Memoon Solves It
Memoon worked through the list, one bug at a time. Reproduce: the dancer flew upward. Isolate: she clicked only the dancer's script; still upward. Hypothesise: wrong direction? She ticked the direction checkbox: 0. Noodles had changed point in direction 90 to 0. Fix one number. Test: the dancer entered from the left. One down.
The lanterns were using when green flag clicked; she changed it back to when backdrop switches to night. The stars had change x instead of change y. The flower's turn block had been dragged outside the inner loop. Each time she changed one thing and tested again.
Then she ran the whole show. Perfect. To be sure, she tested the edge cases: Beautine pressed against both edges of the stage, the announcer spoke for twice as long, Bop pressed f three times in a row. Everything held.
Noodles' ears lifted. "You fixed my mess."
"We fixed it," said Memoon, handing him the rubber duck. "You're on duck duty. Every programmer needs one."
❓ Check Your Knowledge
- What is a bug? What is debugging?
- What happened in 1947 with Grace Hopper's team?
- List the five steps of the debugging method in order.
- Why should you change only one thing at a time when fixing a bug?
- How can a say block help you debug?
- What is rubber duck debugging?
- What is the difference between a normal case and an edge case? Give an example of each for a quiz program.
- True or false: if a program works once, it has no bugs.
- Name the kind of bug in Bug 2 above, and explain the fix.
- Challenge: A sprite should say Hello and then move, but it moves first. Write two different possible causes (hypotheses) and how you would test each one.
📚 Word Bank
- Bug: a mistake in a program that makes it behave wrongly.
- Debugging: finding and fixing bugs.
- Reproduce: make a bug happen again on purpose.
- Isolate: narrow down where a bug is.
- Hypothesis: an educated guess about the cause of a problem.
- Print debugging (tracing): showing values while a program runs to see what's happening.
- Rubber duck debugging: explaining code out loud to find mistakes.
- Testing: running a program in different situations to find bugs.
- Edge case: a test at the limits of what a program should handle.
- Version: a saved copy of a project at one point in time.
Keep learning with Byte by Byte
Explore Book 4: Office Apps and First Programs. Find the ebook, purchase details and teaching slides on MemuniCity.
Comments
Post a Comment