The scientific approach to debugging
Treat debugging as a hypothesis-based experiment. Make detailed
notes of your procedures and observations. It should be a systematic
process turning reality into theory and applying that theory to fix
the problem.
Devil debugging (How not to debug)
- Scatter print statements everywhere
- Try things at random till the bug goes
- Never backup earlier versions
- Don't bother understanding what the program should do
- Use the most obvious fix - the symptom not the problem (e.g. hack in the right answer)
Rules for debugging
- Fix the problem not the symptom
- Understand what the problem is
- Understand what the code should do
- Predict how the fix will address the problem
As with all science:
- Make an observation
- Propose a hypothesis
- Experiment to test the hypothesis
- Make an observation
- If the observations do not match the hypothesis, Go to step 2
Explicit debugging
- Don't keep it all in your head
- Write down your hypotheses
- Record examples of Inputs, Expected output and Actual output
- The process itself will structure your thinking - talking the bug
through with other people will help in the same way
- Discuss your ideas! Kernigan and Pike describe a university where
students were required to describe their problems to a teddy bear
before they could speak to a person!!!
- Play Mastermind - you are making hypotheses, testing them and
having the results recorded