Debugging is finding why a program does something other than what you meant, and fixing the cause rather than the symptom. It is a method, not luck: observe, form a hypothesis, test it, repeat. Programmers spend more time here than writing new code, so the skill pays back every day.
Key concepts
- Reproduce first: a bug you can trigger on demand is half solved; shrink it to the smallest failing case.
- Hypothesis and experiment: change one thing at a time and predict the result before you run it.
- Bisection: halve the search space, over code, inputs or commits, until the culprit is alone.
- Observation tools: debugger, logs, assertions, core dumps and tracing show state without guessing.
- Root cause: ask why until the answer is a design flaw, then add a test that would have caught it.
Vocabulary
- Breakpoint: a place where the debugger pauses the program.
- Stack trace: the chain of calls that led to the current point.
- Heisenbug: a bug that changes or vanishes when you observe it.
- Regression: something that worked and no longer does.
What to study, in order
- Use a debugger on a small program: breakpoints, stepping, inspecting variables and the call stack.
- Read stack traces and error messages completely, from the first line, before searching.
- Practise bisection with your version control tool on a regression.
- Learn memory and thread debugging tools for your language, such as sanitizers and race detectors.
- Explain a bug aloud to someone, or to a rubber duck, before changing any code.