Lite / Building Machines That Respond / Week 8
Reproduce, hypothesise, test: hunting logic bugs
A robot's speed log says 240 m/s. Nothing crashed. Where do you even start?
Why should I care?
Logic bugs give wrong answers quietly. Guessing and changing things at random wastes hours and adds new bugs. A scientific method, applied to code, finds the cause reliably, and the test you write afterwards stops it coming back.
- Apply a systematic method: reproduce, hypothesise, test, fix, keep the test.
- Use print probes to check a hypothesis about where values first go wrong.
- Find and fix off-by-one, indentation and unit-mismatch bugs.
Predict
readings = [20.5, 21.0, 22.5, 24.0] but a loop uses range(1, len(readings)) and divides by len(readings). What does it print?
- 22.0
- 16.875
- An IndexError
Guess first, then reveal
16.875. range(1, 4) gives 1, 2, 3, so readings[0] = 20.5 is never added: 67.5 / 4 = 16.875. The true mean is 22.0. range(len(readings)) fixes it.
Discover
Work through 'Mean too low', 'Count stuck at 0' and 'A very fast robot'. For each: state a hypothesis in one sentence, place a print probe to test it, then pick the faulty line. Write down the probe that convinced you.
What the interactive shows (text version)
In the bug hunt, 'Mean too low' prints mean: 16.875 instead of 22.0; a probe after line 3 shows i taking 1, 2, 3, never 0, so line 3's range(1, ...) is the bug. 'Count stuck at 0' prints 0 instead of 3; a probe shows line 6 runs once with r = 48, because return is inside the loop. 'A very fast robot' prints [240.0, 260.0, 240.0] m/s for distances in millimetres; the speeds are really 0.24–0.26 m/s.
Understand
The method
- Reproduce: find a small input that always shows the bug. 2. Hypothesise: 'I think the first reading is never added.' 3. Test the hypothesis: add a print, or try an even smaller input. 4. Fix the cause, not the symptom. 5. Keep a test that would have caught it.
Print probes
A print just after a suspect line shows the values at that moment: print("i", i, "total", total) inside a loop. If i starts at 1, the hypothesis is confirmed. Remove or comment out probes once the bug is fixed; keep the assert you add.
Three classic logic bugs
Off-by-one: range(1, n) skips index 0; range(n + 1) runs one too far. Indentation: a return inside a for loop returns after the first item. Units: millimetres divided by seconds labelled m/s is 1000 times too big. The first two show in a probe of the loop; the third shows in a units check.
Explain it another way
Intuitive
Like finding which bulb has failed in a string of fairy lights: you test halfway along, then halfway again, rather than replacing bulbs at random.
Technical
Debugging is hypothesis testing. Each probe or reduced input should split the possible causes. Fixing without a confirmed cause risks masking the bug; a regression test prevents it returning.
Applied
Engineering teams write a short bug report for every serious fault (symptom, cause, fix, test). The project brief this term asks for exactly that, because it shows how you think.
Worked example 1
count_over([48, 52, 55, 49, 61], 50) returns 0 instead of 3. Find the bug.
Reproduce: it always returns 0. Hypothesis: the loop stops early. Probe after the if: it prints once, with r = 48. So the function returns after the first reading: return count is indented inside the for loop. Fix: un-indent it to the for's level. Keep assert count_over([48, 52, 55, 49, 61], 50) == 3.
Common mix-ups
Debugging means changing things until it works.
✓ Debugging means forming and testing hypotheses until you know the cause, then fixing that cause.Once it's fixed, the bug is gone for good.
✓ Bugs come back when code changes. A test that would have caught it keeps it gone.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Take any program you wrote this term, plant a bug in it (or ask a friend to), and find it using the method. Write a four-line bug report: symptom, hypothesis, test that confirmed it, fix. Add the assert that would have caught it.
Reflect
Describe a time you fixed something by guessing. How would the method have changed what you did?
Write your answer on paper or in your own notes. Lite never stores what you write.