Unit 16 · lesson

Debug From the First Divergence, Not the Last Symptom

A bad output line may be the last visible symptom of a defect introduced much earlier.

Suppose a report says:

completion = 0.0%

Possible causes include wrong raw input, parsing/default behavior, reversed arguments, integer division, collection state, branch selection, or formatting.

Changing the print statement would be a guess.

Trace boundary by boundary

Concept flow

Debug from the first divergence, not the last symptom

Trace expected and actual state across boundaries until they first differ.

  1. REPRODUCEpreserve input, expected behavior, and actual behavior
    trace
  2. BOUNDARIEScompare state at meaningful handoff points
    find
  3. FIRST DIVERGENCElocate where actual state first differs
    hypothesize
  4. CONTROLLED CHANGEchange one responsible cause
    rerun
  5. REGRESSIONverify the fix without breaking nearby behavior

Record expected and actual at each meaningful boundary. Stop when you find the first divergence.

If raw and parsed state are correct but calculation differs, do not keep investigating the input parser.

Reproduce before fixing

A defect you cannot reproduce is hard to verify as fixed.

Preserve:

input/state
expected behavior
actual behavior
version/conditions

Then create the smallest repeatable case possible.

A ten-line reproducer can be more useful than a 500-line application where the bug happens "sometimes."

Use hypotheses, not random edits

Write:

Hypothesis: integer division truncates before assignment to double.
Prediction: changing one operand to double will change 0.0 to 70.0 for 7/10.
Experiment: change only the operand type/cast.
Result: ...

If the result rejects the hypothesis, restore the baseline and form another one.

Random edits destroy causal evidence.

Debugger tools are optional; state reasoning is not

An IDE debugger can step through code, inspect variables, and pause at breakpoints. Print tracing can expose state in a browser environment.

The tool changes. The reasoning remains: where did actual state first diverge from expected state?

Create a boundary trace

Take a program with at least four layers/methods. Introduce one defect in an early layer and let it produce a later wrong result.

Document every boundary until the first divergence appears. Then make one correction and rerun the same trace plus regression tests.

Evidence

Your debugging record must include reproducible input, expected and observed behavior, a boundary table, first divergent state, hypothesis, controlled experiment, correction, and regression result.

A screenshot of the final correct output cannot replace this evidence.