๐ WAYPOINT LESSON
Trace tables and the debugging loop
โญ beginnerโณ 12 min read๐ Lesson 63 of 256
Reproduce, classify, trace state, assert mid-way, fix the cause.
Systematic debugging โ a loop, not a hunt:
- Reproduce on the smallest failing input. A bug you can't shrink, you can't fix.
- Classify: compile / runtime / logical (Module 16.1).
- Trace state: walk the code by hand writing every variable's value on every line โ a trace table. The row where reality diverges from intention is the bug.
- Assert mid-way: print or check intermediate values ("at this
point,
sumshould be 6"). - Fix the cause, not the symptom: if a loop is off-by-one, change the bound โ don't add a special case downstream.
Trace-table example โ the divergence is line 4:
| line | i | sum | want | | ----------- | - | --- | ------ | | sum = 0 | โ | 0 | 0 | | sum += i | 1 | 1 | 1 | | sum += i | 2 | 3 | 3 | | sum += i | 3 | 6 | 9? (goal: sum of 1..3 = 6) |
When your hand-trace of the intended algorithm disagrees with what the code computes, you've found the exact line to change.
โก Now practice
Ready to CodeTesting and debugging repsLeap logic, middle value, crash guards, boundary repair, grade chains.
4 challenges ยท ยท ~50 min