Teaching article

Debug the first wrong state, not just the final destination

Locate the first mismatch between intended and actual position.

A route may end far from its goal because of one early turn. Trace the commands in order and locate the first mismatch, giving the repair a specific target instead of repeatedly changing the last instruction.

Compare expected and actual states after each command. Repairing the first divergence is more informative than repeatedly guessing a final correction.

Find where the expected and actual paths diverge

  1. Trace commands slowly.
  2. Mark the first divergence.
  3. Replace one command.

An incorrect turn at command three explains later wrong squares.

Changing the final step may hide the earlier error. Where did the first mismatch occur?

Use a trace table.

Resources

Further reading

Read next

Explore Coding and computational thinking. Browse all teaching articles.