Debugging: Find and Fix Your Program's Bugs
Byte the friendly robot kneels beside a winding floor maze, holding a glowing checklist and pointing at one wrong arrow card while a toy rover waits to move
- Define debugging as testing a program to find and fix a mistake.
- Run a program step by step to locate exactly where it goes wrong.
- Change one step at a time and test again to check the fix.
- Explain why guessing and changing many steps at once makes bugs harder to find.
Key terms
- Bug
- A wrong step in a program.
- Debugging
- Finding and fixing a program's mistake.
- Step
- One instruction the computer follows in order.
- Test
- To run a program and watch it.
What Debugging Means
A program is a list of steps the computer follows in order. Sometimes one step is wrong, so the program does the wrong thing. That mistake is called a bug. Finding the bug and fixing it is called debugging. Every coder debugs, so it is a normal and helpful skill to have.
The Four Detective Moves
Good debugging has four moves. First, run the program and watch what really happens. Second, find the exact step where things go wrong by checking one step at a time. Third, fix only that one wrong step. Fourth, test again to make sure the program works now. Slow and careful wins.
Change Only One Step
It is tempting to change many steps at once, but that makes bugs harder to find. If you change lots of steps together, you cannot tell which change helped or which one made a new problem. Change one step, test, and check. That way you always know exactly what your fix did.
Worked examples
Rover should go UP, RIGHT but goes UP, UP.
- Check step one: the first UP matches the plan, so it is fine.
- Check step two: the plan says RIGHT but it says UP, so that is the bug.
Answer: Change the second step to RIGHT, then test again.
Put these debugging moves in order.
- First run the program and watch it.
- Then find the wrong step, fix only it, and test again.
Answer: Run, find, fix, then test again.
Activity
Byte mixed up the debugging steps! Drag them into the correct order to fix a buggy program
Practice
List the four debugging moves in the correct order.
Explain why changing many steps at once is a bad idea.
Common mistakes to avoid
- Guess and change many stepsChange one step at a time so you know what fixed the bug.
- Restarting the computer fixes bugsRestarting does not change your steps, so the bug stays there.
Check your understanding
What does 'debugging' mean?
A bug is hiding somewhere in your steps. What is the smartest way to find it?
Your rover should go UP, RIGHT but it goes UP, UP and misses the star. The first UP is correct. Where is the bug?
Recap
Debugging means finding and fixing a wrong step in a program. Run the program, find the exact step that goes wrong, fix only that one step, and test again. The first step that surprises you is your bug, so go slow.
Reflect
What surprising step would you check first when debugging?