Find and Fix the Bug
Byte the friendly robot stands on a colorful floor mat, pointing a glowing finger at a row of arrow cards while a little toy car waits at the start.
- Read each step of a short plan in order.
- Spot the one step that makes the program go wrong.
- Fix the broken step so the plan works.
- Test the plan again to check that the fix worked.
Key terms
- program
- a list of steps that a robot or game follows in order
- bug
- a step that is wrong and makes the program do the wrong thing
- debugging
- the job of hunting for a bug and then fixing it
- test
- running the program again to check that it works
What a Bug Really Is
A bug is not a creepy crawly insect. In computing, a bug is just a step that is wrong inside a program. When the program runs, that wrong step makes it do something you did not want. The good news is that one bug usually lives in only one step. Most of the steps are still doing their job perfectly, so you do not need to throw the whole plan away. You only need to find the one step that misbehaves.
How to Hunt for the Bug
Hunting for a bug is like being a detective. You read the steps slowly, one at a time, and point to each one. Say what each step should do, then watch what actually happens. As long as the program is doing what you expect, keep going. The very first step that does the wrong thing is your bug. Going step by step like this is the smartest way to find a bug, because guessing wildly usually misses it.
Fix It, Then Test It
Once you find the bug, fixing it is simple: swap the one wrong step for the right step and leave the working steps alone. But you are not finished yet. You must test the program again by running it from the start. Testing proves the fix really worked and shows that you did not accidentally create a new bug. Real programmers test after every fix, every single time, because a fix is only finished once the program runs the right way.
Worked examples
Find the bug that stops the toy car reaching the star.
- Read step 1: UP arrow moves the car closer to the star, which is what we want, so keep going.
- Read step 2: DOWN arrow moves the car away from the star, which is wrong, so this step is the bug.
- Swap the DOWN arrow for an UP arrow, then run all the steps again to test.
Answer: Step 2 (the DOWN arrow) is the bug; swap it for an UP arrow and the car reaches the star.
Activity
Help the toy car reach the star. One arrow is wrong. Find it and swap it for the correct arrow.
Practice
A plan says: open the door, walk outside, then unlock the door. Find the step that is in the wrong place.
Tell a friend why we test the program again right after we fix a bug.
Common mistakes to avoid
- If a program has a bug, throw it all away.Only the one broken step needs fixing; the other steps already work and should be kept.
- Once you change a step, the fix is done.You must test the program again to make sure the fix worked and no new bug appeared.
Check your understanding
A program does the wrong thing. What do we do first?
We found the bug in step 3. What is the best fix?
After we fix the broken step, what should we do?
Recap
A bug is one wrong step inside a program. Instead of starting over, you hunt for the bug by reading the steps one at a time and watching what happens. When you find the step that goes the wrong way, you swap it for the right step and then test the program again to be sure the bug is gone.
Reflect
When something you built did not work, how did you find and fix the problem?