Debugging Categories
Writing code that runs is not enough; it must produce correct results. The hardest bugs are silent: the code runs without errors but produces wrong answers.
| Bug Type | Example | Fix |
|---|---|---|
| Syntax | Missing colon | Read error |
| Runtime | Division by zero | Add guard if x != 0 |
| Logic | Off-by-one bounds | Trace small example |
| Numerical | Float drift | Smaller step |
Common pitfall: Fixing the crash site often leaves the root cause untouched. Trace the bad value back to where it was born; the crash is usually just the victim.
Strategies and Validation
Use code structure to catch mistakes early. Print intermediate values (print(f"x={x}")), use assertions to catch invariants (assert energy > 0), and test known inputs.
Validate your work systematically:
| Level | Method | Purpose |
|---|---|---|
| 1 | Unit tests | Check functions |
| 2 | Conservation | Check energy/momentum |
| 3 | Convergence | Halve to check error |
| 4 | Comparison | Match analytics |
Conservation checks are your best defense against silent bugs.