"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?" - Brian Kernighan in "The Elements of Programming Style" (1974)
"The most effective debugging tool is still careful thought, coupled with judiciously placed print statements." - Brian Kernighan in "Unix for Beginners" (1979)
"Hercule Poirot's methods are his own. Order and method, and 'the little grey cells'." - The Big Four, in Agatha Christie
"Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth." - Sherlock Holmes, in Doyle Arthur Conan
"Debugging is like being the detective in a crime movie where you are also the murderer." - Filipe Fortes
"By June 1949 people had begun to realize that it was not so easy to get programs right as at one time appeared. ... the realization came over me with full force that a good part of the remainder of my life was going to be spent in finding errors in my own programs." - Maurice Wilkes
1. How to avoid debugging?
- Test Driven Development - write tests in the the Reinterpret the Problem phase (e.g. using
- Use the incremental development method
2. How debug should not look like?
- Making random guessing and trying it out
- Staring on the code until an insight will appear
- Copy-paste of the error message to a search engine without thinking
- Asking someone else
2. Challenge - Three models
- Problem - What is the code supposed to do?
- Program (Python) - What is the code actually doing?
- Programmer - What do I think that the code is doing? (expectations, the mental model of the program)
3. Fundamental Mindset - The Scientific Method - "Experimental" / "Detective" / "Doctor"
- Debugging systematically
- Everyone is a suspect (Except Python)
Terminology (IEEE 610.12-1990)
- Failure - occurs when a program’s output does not match documented output requirements, or the programmer’s mental model of output requirements.
- Fault - which is a runtime state of a program that either is or appears to be incorrect (as in assuming a lack of output from a debugging print statement to mean the code was not reached). Faults occur as a result of errors.
- Error - which are program fragments that do not comply with documented program specifications or the programmer’s mental model of specifications (such as a missing increment or misspelled variable name).
The Four Questions to Answer
- What is the bug?
- Why the bug happens? What is the cause to the bug? What is the root cause?
- Where in the code does the cause of the bug lay?
- What-if I change the code like that...?
Where to Look for Bugs Especially?
- In the main algorithm section
- In the gap between code sections
Empirical Approach for Debugging
Diagnose (Why? Where?)
- The Bug Itself
- Error messages & Traceback
- What might cause this error
- How to uncover the root cause
- Reading the Code (with a critical eye)
- Results from the Test phase
- Debugging Toolbox
- The Bug Itself
Model the Cause(s) of the Bug
- Formulate Hypothesis
- Manipulate / Check
- Accept / Reject
- Keep Records
Use Binary Search
Repeat until you fix the bug
Reflect - Debug yourself
- Comment in/out
Rubber Duck Debugging
Tackling the Mental Model
Two Great Resources from "How to Think Like a Computer Scientist" Interactive Book:
Did I fix the Bugs?
All rights reserved.