Subsection 7.2.1 The Scientific Debugging Method
Effective debugging is a systematic process, and it has five steps, which are best taken in order:
-
Reproduce the problem reliably. A bug you cannot reproduce is almost impossible to fix. Note the exact inputs, conditions, and sequence that trigger it.
-
Form a specific hypothesis. “Something is wrong with the ADC” is not a hypothesis; “the ADC clock is never enabled, so the data register always reads zero” is. It names a cause you can test.
-
Change one thing at a time. If you change five things and the bug disappears, you do not know which change mattered. The root cause is still unknown, and the bug is free to return.
-
Bisect. Confirm the program is healthy at its midpoint (a
printf, a breakpoint), then narrow to the half that contains the fault. Each test halves the search. -
Keep notes. Once you have been stuck for more than a few minutes, keep a record of what you tried and what you observed. It prevents re-testing the same idea, and it is exactly what a TA or instructor will ask you for.
