Lite / Discovering How the World Works / Week 21
Debugging: find the step that went wrong
Your robot crashes on step 4 of 10. Is the bug in step 4?
Why should I care?
Every engineer spends a large part of their time debugging. The best ones don't guess; they trace. Stepping through a program, writing down what happens, and comparing it with what should happen is the single most useful habit you can build.
- Trace a program one step at a time to locate a bug.
- Explain that the bug may be before the step where things go wrong.
- Fix a bug and re-test the whole program.
Predict
A program says: Forward, Forward, Turn left, Forward… The robot (starting at (0, 0) facing north) crashes on the 4th instruction. Where is the bug most likely to be?
- In instruction 4
- In instruction 3: it should have been Turn right
- In instruction 1
Guess first, then reveal
Instruction 3. Turning left at (0, 2) points the robot west, off the edge of the grid, so the next Forward crashes. The crash shows up on step 4, but the mistake was step 3. Bugs often appear after the step that caused them.
Discover
Press 'Load a buggy program', then use Step to run it one instruction at a time. Find the instruction that caused the crash, fix it with the arrow and delete buttons, and run it again from the start.
What the interactive shows (text version)
The grid robot can be replaced by tracing on squared paper: the buggy program Forward, Forward, Turn left, Forward… starts at (0, 0) facing north, reaches (0, 2), turns to face west, then crashes off the left edge on instruction 4. Changing instruction 3 to Turn right (and the later Turn right to Turn left) makes it reach the flag at (4, 4).
Understand
Bug: where it shows vs where it is
A bug is a mistake in a program. Where things visibly go wrong (the crash) is often later than where the mistake was made (a wrong turn earlier). Debugging means working backwards from the symptom to the cause.
Trace, don't guess
Run the program one step at a time. After each step, write down what the robot should be doing and what it is doing. The first step where they disagree is your bug. This is called tracing.
Fix, then test everything
Changing one instruction can break a later one, because every step after it now starts from a different place. After a fix, always run the whole program from the start again, not just the part you changed.
Explain it another way
Intuitive
If you get lost walking to a friend's house, the place you notice you're lost isn't where you went wrong. You retrace your steps to find the wrong turn.
Technical
Debugging compares the expected state with the actual state at each step. The bug lies at or before the first step where they differ. Tools called debuggers let programmers step through code exactly like the Step button.
Applied
Spacecraft software is tested by running whole missions in simulation, step by step, before launch. When something goes wrong, engineers replay the log to find the first moment the spacecraft's state disagreed with the plan.
Worked example 1
The program 'Forward, Forward, Turn left, Forward, Forward, Forward, Forward, Turn right, Forward, Forward' crashes on step 4. Trace it and fix it.
Start (0, 0) north. 1: (0, 1). 2: (0, 2). 3: Turn left: facing west. 4: Forward would go to (−1, 2): off the grid. Bug at step 3. Change it to Turn right: now facing east, steps 4–7 go to (4, 2), step 8 should become Turn left to face north, and steps 9–10 reach (4, 4). Fixing one turn meant fixing the other too. Re-run from the start to check.
Common mix-ups
The bug is always on the line where things go wrong.
✓ It's often earlier. Trace back to the first step where expected and actual differ.Once you've fixed a bug, you're done.
✓ Re-test the whole program; fixes can cause new problems further on.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Write a 6-instruction program with one deliberate bug for a friend. Swap programs, trace each other's step by step on squared paper, and write down the step number where expected and actual first disagree.
Reflect
What is the difference between guessing at a fix and tracing to find a bug? Which did you do first?
Write your answer on paper or in your own notes. Lite never stores what you write.