Phase 1: Prepare
Step 1 Warm-up recall
From M7.L3: what is a boundary test?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- reproduce a fault (Foundation);
- narrow it down with print and tracing (Core);
- fix, retest and keep a test (Core).
Phase 2: Teach
Step 3 A familiar example
A mechanic does not replace parts at random. She hears the fault, narrows it to one system, changes one part and tests the car. Debugging follows the same discipline.
Step 4 Clear explanation
Debugging is finding and fixing a cause. A method beats guessing.
- Reproduce the fault with the same input every time.
- Narrow it down: print values at key points, or trace the code on paper, until you find the first line where the value is wrong.
- Change one thing at a time.
- Retest with every earlier test, not only the one that failed.
- Keep the failing case as a test so the fault cannot quietly return.
def average(values):
total = 0
for v in values:
total = total + v
return total / len(values)
print(average([2, 4, 6]))0.6666666666666666The right answer is 4.0, so the output is wrong, with no error message. Printing total inside the loop shows 2 after the first item only. The return sits inside the loop, so the function ends after one item. Moving it out by one indentation level gives 4.0. Then add assert average([2, 4, 6]) == 4 as a kept test.
If you cannot reproduce a fault, say so and record what you know. Dismissing an odd result as a glitch is how faults ship.
Step 5 Worked example (4 marks)
A loop adds the items of [1, 2, 3] but prints 3 instead of 6. Using the method, what is a sensible first move?
- The answer is wrong with no error, so reproduce with the same list. (1 mark)
- Print the total after each step to see where it goes wrong. (1 mark)
- The total should be 1, 3, 6; if it shows 1, 2, 3 the line uses = instead of adding to total. (1 mark)
- Fix that line only, retest, and keep the case as a test. (1 mark)
Go deeper: the professor's notes
Same input.
Print or trace.
A test.
Order
| Step | Why |
|---|---|
| one change at a time | know what worked |
| keep a test | fault cannot return |
At work: A wrong total
The total is wrong and no error appears.
- Print the total each step.
- Find the first wrong value.
What a careful practitioner decides. A method beats guessing.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. The first step in debugging is to:
Same input every time.
2. You should change:
Then you know what worked.
3. After a fix, you should rerun:
Check nothing else broke.
4. A fault that cannot be reproduced should be:
Keep notes.
Phase 4: Close
Step 7 Recap
- Reproduce, narrow, change one thing, retest, keep a test.
- Print or trace to find the first wrong value.
- Never dismiss an odd result as a glitch.
Step 8 Exit ticket (ungraded)
List the five steps of the debugging method in order.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Take a program that gives a wrong total and trace it on paper to find the first wrong value.
In industry: Mars Pathfinder: debugged from Earth
A few days after landing in July 1997, Pathfinder began total system resets. Engineers recreated the fault on a replica in their laboratory with tracing turned on, found priority inversion, and uploaded a short change that stopped the resets. Resets seen in pre-flight tests had been put down to a hardware glitch.
Link to this lesson. Reproduce the fault with the same conditions, trace to the first wrong step, and never write off an odd result as a glitch.
Source: Mike Jones, What really happened on Mars Rover Pathfinder, RISKS Digest 19.49 (1997); Rapita Systems
Common mistakes
| Mistake | Why it fails |
|---|---|
| Changing several things at once | You will not know which change helped. |
| Not keeping a test | The fault may return. |
| Blaming a glitch | Keep notes and try to reproduce. |
Limits. Larger programs use a debugger that steps through the code; the thinking is the same as tracing on paper.