Courses / Computer Science I
Programming I

Debugging Logic and State

Computer Science I 234 words Free to read

Hunting the Bug

A bug is a mismatch between what a program does and what it should do. Debugging is the disciplined process of locating and fixing that mismatch by searching through the program's state—the current values of its variables—for the point where things first go wrong.

Effective debugging relies on two everyday tools:

ToolMechanismPurpose
Print debuggingInserting statements showing variable valuesCrude but effective visibility
DebuggerSetting breakpoints and stepping throughPrecise state inspection

Both tools serve the same goal: making the invisible state visible so you can see where it diverges from your expectations.

The Debugging Routine

A reliable routine prevents wasted time and blind guessing:

  1. Reproduce: Find an input that triggers the bug consistently. Unreproducible bugs cannot be reliably fixed.
  2. Localize: Narrow down where the state first becomes wrong. Confirm correct state early and squeeze the gap.
  3. Understand: Determine why that specific line produces the wrong value. Do not fix blindly.
  4. Fix and verify: Change the code and re-run your tests to confirm the fix works without breaking anything else.

The golden rule is to reason from evidence, not assumption.

Pitfall: Changing code randomly hoping the bug disappears ("shotgun debugging"). Without observing actual state, you cannot know if you fixed the real cause.
Guesses scatter around the bug; evidence closes in on it

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

11practice questions
2interactive scenes

Programming I