A lesson on debugging techniques to help students find and fix mistakes in their programs.
Imagine you programmed a robot to make you a peanut butter sandwich, but it put the peanut butter on the outside of the bread! How do you find the mistake and fix it?
In the world of coding, a bug is simply a mistake in your program that prevents it from working the way you want. The process of finding and fixing these mistakes is called debugging. Did you know the term came from a real moth found inside an early computer in ?
Computers aren't smart enough to know what you meant to do; they only do exactly what the blocks tell them to do. If your character is supposed to jump but it spins instead, you have a bug! Being a Code Detective means looking at your blocks closely to see where the instructions went wrong.
Quick Check
If your code has a 'bug,' does it mean your computer is broken?
Answer
No, it just means there is a mistake in the instructions (the code) that needs to be fixed.
Most bugs fall into two categories. First is the Wrong Sequence. This is when you have all the right blocks, but they are in the wrong order. For example, if you tell a sprite to 'Move' before you tell it to 'Point in Direction,' it might walk off the screen!
The second common mistake is Missing Blocks. This happens when you forget a step, like forgetting to tell a loop to 'Repeat' or forgetting the 'Start' block. Even a tiny mistake, like setting a value to instead of , can change everything.
A student wants to draw a square with sides. Here is their code: 1. Move steps 2. Turn degrees 3. Move steps 4. Turn degrees 5. Move steps
The Bug: The sprite only draws sides! The Fix: Add one more 'Move' and 'Turn' block to complete the side.
Quick Check
What is it called when your blocks are in the wrong order?
Answer
A sequence error (or wrong sequence).
When your code is long, it's hard to see the bug just by looking. Code Detectives use a strategy called Step-by-Step Testing. Instead of running the whole program at once, you run one block at a time.
Watch your character closely after every single block. If the character does exactly what you expect for the first three blocks but does something weird on the fourth, you've found your bug! You can also use Wait blocks to slow the code down so your human eyes can keep up with the fast computer.
Your sprite is supposed to hide, wait seconds, and then show up again. But it never reappears! 1. When Green Flag clicked 2. Hide 3. Wait seconds 4. Move steps
The Bug: The 'Show' block is missing! The Fix: Add a 'Show' block after the 'Wait' block so the sprite becomes visible again.
You want a sprite to spin times and then stop. 1. Repeat times: 2. Turn degrees 3. Move steps 4. Change 'Score' by
The Bug: If the sprite keeps spinning forever, check if you accidentally put the 'Repeat' block inside another 'Forever' loop! The Strategy: Pull the blocks out of the loop and test them one by one to see if the 'Repeat' logic is working correctly.
What is the first thing a 'Code Detective' should do when their program doesn't work?
If you want to draw a triangle but your sprite draws a straight line, what is the most likely bug?
True or False: A 'Sequence Error' means you have the right blocks but they are in the wrong order.
Review Tomorrow
Tomorrow, try to explain to a friend why a computer scientist might be happy to find a 'bug' (Hint: It means they are one step closer to a perfect program!).
Practice Activity
Open a project you've already finished. Intentionally move one block to the wrong place or delete one block. Then, try to 'debug' it to get it back to normal!