Definition
Your instructions for reaching the market say "turn left at the post office", but your friend ends up at the railway station. Somewhere, something went wrong. How do you find it?
Debugging is the process of finding and fixing mistakes, called bugs, in a set of instructions or a system. It involves noticing that something is wrong, finding exactly where the problem is, working out why it happens, fixing it and testing again.
In simple words
If a cake comes out flat, a good baker does not throw away the whole recipe. They ask: did I forget the baking powder? Was the oven hot enough? They check one thing at a time until they find the cause, fix it and bake again.
Why it matters
Everyone makes mistakes; skilled people are good at finding and fixing them. Debugging is a core skill in computing, science and everyday life, and AI systems need constant debugging when they give wrong or unfair answers.
Key terms
- Bug
- A mistake that makes a system or set of instructions behave wrongly.
- Debugging
- Finding and fixing bugs.
- Test case
- A specific example used to check whether something works, with a known correct answer.
- Expected result
- What should happen if everything works.
- Actual result
- What really happens when you test it.
- Edge case
- An unusual or extreme input, such as zero, an empty list or a very large number, where bugs often hide.
Explained in detail
Where the word comes from
Engineers had called faults "bugs" since at least the 1870s, but a famous story made the word popular in computing. In 1947, operators of the Harvard Mark II computer found that a moth stuck in one of its switches was causing errors. They taped it into their logbook with the note "first actual case of bug being found". The logbook page is now kept in a museum in the United States. Grace Hopper, a pioneering computer scientist who worked on that team, often told the story.
A step-by-step method
| Step | What you do | Example: directions to the market |
|---|---|---|
| 1. Notice | Compare expected and actual results | Expected: market. Actual: railway station. |
| 2. Reproduce | Make the bug happen again on purpose | Follow the directions yourself |
| 3. Locate | Check step by step to find where it goes wrong | Steps 1 to 3 are fine; step 4 sends you the wrong way |
| 4. Understand | Work out why | There are two post offices; you meant the second one |
| 5. Fix and retest | Change one thing, then test again | "Turn left at the post office opposite the bank" |
Useful debugging habits
- Change one thing at a time, so you know which change fixed it.
- Explain your steps out loud to someone, or even to a toy. Programmers call this rubber duck debugging; saying each step often reveals the mistake.
- Test edge cases: zero, nothing, the largest possible value, an unusual name.
- Keep a note of what you tried, so you do not repeat it.
- After fixing, check that you did not break something else.
Debugging AI systems
AI systems have their own kind of bugs. A model might work well for most users but fail for people with a particular accent, or for photos taken in poor light. Debugging means collecting the cases where it fails, looking for what they have in common, and fixing the cause, often by adding better or more varied training examples. The trainer activity in the Machine Learning module showed exactly this: the model failed on small dogs until small dogs were added to its examples.
Good testers also try to break things on purpose before users do.
How it works
- Write down what should happen and what actually happened.
- Make the problem happen again.
- Go through the steps one at a time to find where it goes wrong.
- Work out why, then change one thing to fix it.
- Test again, including unusual cases.
Try it yourself
The average that was always wrong
Priya writes steps for finding the average of her test marks, but the answer looks too small.
Find the bug in the morning routine
Read these steps and find what is wrong.
- 1. Wake up. 2. Put on shoes. 3. Put on socks. 4. Brush teeth. 5. Eat breakfast.
- Act out the steps exactly as written.
- Write down the expected and actual result.
- Fix the bug and act it out again.
Think about it
- Which step was the bug in, and what kind of mistake was it?
- Can you find a second, smaller improvement?
Common mistakes
- Changing many things at once, so you cannot tell what fixed it.
- Testing only the easy, normal case.
- Giving up or starting from scratch instead of locating the problem.
Check yourself
Summary
- Debugging means noticing, reproducing, locating, understanding and fixing mistakes.
- Change one thing at a time, explain aloud and test unusual cases.
- AI systems are debugged by studying where they fail and improving their data.
Finished reading?
Sources: BBC Bitesize: Introduction to computational thinking · CS Unplugged