Debugging a Program by Testing One Step at a Time
Byte the robot sits at a wooden workbench covered in colorful recipe cards, carefully comparing a freshly baked but slightly burned cookie to the perfect golden cookie drawn on the card, scratching their head with a pencil and circling step 3 with a red marker.
- Explain what a bug is and why programs sometimes do the wrong thing.
- Identify which step in a program caused an unexpected result.
- Compare what a program actually does to what you expected it to do.
- Predict what will happen when a bug is fixed and the program is run again.
Key terms
- Bug
- A mistake that makes a program go wrong.
- Debugging
- Hunting for and fixing a program's mistake.
- Expected
- What you thought the program would do.
- Actual
- What the program really did when it ran.
What a Bug Is
A bug is a mistake hiding in your program. When a bug is there, the program does the wrong thing, like baking cookies that come out burned. Bugs are normal and every programmer finds them. The good news is that you can hunt them down and fix them one step at a time.
Test One Step at a Time
Do not try to check everything at once, because that gets confusing fast. Instead, check step one, then step two, then step three. At each step, look at what really happened. Keep going slowly until you reach the step where something went wrong. That careful checking is how good detectives work.
Expected Versus Actual
The bug hides in the gap between what you expected and what actually happened. If you expected golden cookies but got burned ones, that gap is your clue. Find the step where the real result is different from the result you wanted. That surprising step is where the bug is hiding.
Worked examples
A drink machine should pour juice but pours water.
- Check step one: did it pick the right cup? Yes, that looks fine.
- Check step two: did it pick juice? No, it picked water. That is the bug.
Answer: The bug is in step two, where it chose water instead of juice.
You expected a blue square but got a blue circle.
- The color blue is right, so that step is fine.
- The shape is wrong, so the shape step holds the bug.
Answer: The bug is in the step that chose the shape.
Activity
Byte's sandwich-making program already has the steps in the right order — tap the one step that caused the sandwich to come out wrong.
Practice
Describe what you would check first to find a bug.
Explain what expected and actual results mean in your own words.
Common mistakes to avoid
- Check every step at onceTesting one step at a time helps you find the exact bug faster.
- A program has only one bugA program can have more than one bug, so keep testing after a fix.
Check your understanding
Byte's drawing program is supposed to make a blue square, but it draws a red circle instead. What is the BEST first step to find the bug?
Mia finds a bug in step 3 of her program. She fixes it and runs the program again. The result (output) is still wrong. What should Mia do next?
What does it mean when what a program ACTUALLY does is different from what you EXPECTED it to do?
Recap
Debugging means hunting for mistakes in a program. Test one step at a time and compare what actually happened to what you expected. The step where they are different is where the bug hides, so fix that step and run again.
Reflect
What is one thing a careful detective does when stuck?