Sometimes an algorithm does not work. There is a mistake hidden in the steps. We call that mistake a bug. Finding and fixing a bug is called debugging.
Test, spot, fix, test again
Debugging has a simple pattern you can use for anything:
| Step | What you do |
|---|---|
| 1. Test | Run through the steps and see what happens. |
| 2. Spot | Find the exact step where it goes wrong. |
| 3. Fix | Change that step to make it right. |
| 4. Test again | Run through it once more to check it works. |
A buggy algorithm
This "getting dressed" algorithm has a bug: the shoes go on before the trousers, so the trousers get stuck! A good bug-hunter reads through the steps slowly, one at a time, until they spot exactly where it goes wrong.
Put these steps in the correct order to get dressed:
- Put on your socks
- Put on your trousers
- Put on your shoes
Being a good bug-hunter
Good debuggers go slowly and check one step at a time. They do not rush. When they think they have fixed it, they always test again, because sometimes a fix makes a new bug! Debugging is a skill every coder uses, again and again, every time they write something new.