Debugging Strategies: From Symptoms to Evidence

A program can finish running and still produce an unexpected result. Debugging is the systematic process of locating, understanding, and correcting the cause of an observed program failure. You begin with a symptom, such as an incorrect total, and gather evidence about where the program's behaviour first departs from its intended behaviour. A useful strategy replaces repeated guessing with small questions that execution can answer.

Make the failure repeatable

Before editing, record the input, the actions that trigger the problem, the expected result, and the actual result. Include relevant starting conditions, such as whether a saved file was loaded or a counter already contained data. Without these details, two runs may appear comparable while actually beginning differently.


Reduce the input while preserving the failure. For a long list, try a shorter list that still demonstrates the same discrepancy. A small, repeatable case makes manual calculations and variable inspection easier. If the problem occurs only sometimes, record what changes between successful and unsuccessful runs instead of assuming the cause is random.


Four connected stages show reproduction, localization, investigation, and repair with verification.



Find the first disagreement

Work backward from the wrong output to the values used to produce it. Compare expected and actual values at useful boundaries: after reading input, after a calculation, or before displaying a result. Matching values narrow the search; a disagreement identifies a region worth inspecting.


For a running total, the intended update is:


total after update=total before update+current item value\text{total after update} = \text{total before update} + \text{current item value}


Example: Suppose a program adds a 120-cent item to a total of 300 cents. The expected new total is 420 cents. If the next observed total is 120 cents, the update deserves inspection because the earlier contribution has disappeared.


Form a specific explanation and predict what you would observe if it were true. For example, an assignment might replace the total instead of increasing it. Inspect the value immediately before and after that assignment. A symptom identifies where a problem becomes visible; it does not automatically identify its cause.

Choose observations deliberately

A breakpoint is a selected location where a debugger pauses execution. Pause before a suspicious statement, inspect its inputs, advance execution, and compare its output with your prediction. Check whether the highlighted statement is about to execute or has already executed; many debuggers pause before executing it.


Debugger control

What it usually does

When it helps

Step over

Executes the next statement without entering called functions

Checking an update while keeping the current level of detail

Step into

Enters a called function when source debugging is available

Investigating a calculation inside that function

Continue

Runs until another stopping condition is reached

Skipping execution you do not currently need to inspect


A watch expression helps you follow a changing value. A conditional breakpoint pauses only when a specified condition is true, which is useful when a loop repeats many times. The call stack helps explain how execution reached a function.


Targeted print statements are another option. Label each printed value and include enough context to distinguish repeated loop iterations. Avoid printing everything: a large output can hide the transition you need.



Repair and check the explanation

Once the evidence supports an explanation, make the smallest change that addresses it. Change one suspected cause at a time so that you can connect the result to the edit. Rerun the original failing case, then check relevant nearby cases, such as an empty list, one item, or a boundary value.


A convincing repair explains the original failure and produces the intended behaviour on the cases checked. One successful run does not establish correctness for every possible input. Remove temporary debugging output when it is no longer needed, and keep the useful reproduction details.


What to remember

  • Reproduce the failure before changing code.
  • Compare expected and actual values to find the first disagreement.
  • Choose debugger controls and observations that answer a specific question.
  • Repair one supported cause, then rerun the failure and related cases.

Example: Recovering a Checkout Subtotal

A school fundraiser records item prices as integer cents. Its checkout function should return the sum of all prices, but the function below returns an unexpected subtotal. A trace records variable values in execution order so that you can compare actual changes with expected changes.


Python
def subtotal_cents(prices):
    total = 0
    for price in prices:
        total = price
    return total

prices = [450, 275, 625]


All prices are nonnegative integer numbers of cents. The function should return 0 for an empty list.


a) Calculate the expected subtotal and the actual returned subtotal for the supplied prices. Identify the first loop iteration whose stored total differs from the expected running total.

  1. Calculate the intended running totals. After the first item, the expected total is ________. After the second item, it is ________. Including the third item gives:


450+275+625=1350 cents450+275+625=1350\ \text{cents}


  1. Follow the assignment exactly. Each iteration sets total equal to the current price, producing stored totals of 450, 275, and 625 cents.
  2. Compare the sequences. The first iteration agrees. The second stores 275 cents instead of 725 cents, so this is the first disagreement.

The expected subtotal is 1350 cents, the actual subtotal is 625 cents, and the first disagreement occurs in the second iteration.


b) A debugger pauses immediately before total = price executes in the second iteration. What values should you inspect, and what does stepping over the assignment reveal?

  1. Inspect the values used by the assignment. Immediately before the second update, total contains ________ and price contains ________. The previous iteration supplied the existing total; the current iteration supplies the next price.
  2. Predict the intended update:


new total=450+275=725 cents\text{new total}=450+275=725\ \text{cents}


  1. Step over the assignment. The observed value becomes 275 cents because total = price replaces the previous total. The statement discards the earlier contribution instead of adding the current price to it.

The observation supports a specific diagnosis: the loop overwrites the running total. Merely inspecting the final return statement would show the symptom without showing this transition.


c) Make a single focused repair. Verify the repaired function using the original list, the empty list, and the one-item list [725].

  1. Replace the overwriting assignment with an accumulating assignment:


Python
def subtotal_cents(prices):
    total = 0
    for price in prices:
        total += price
    return total


  1. Rerun the original list from a fresh function call. The stored totals are now:


450 cents,725 cents,1350 cents450\ \text{cents},\quad 725\ \text{cents},\quad 1350\ \text{cents}


The repaired function returns the expected 1350 cents.

  1. Check the two additional cases. For [], the loop never executes, so the initial total of 0 cents is returned. For [725], the single update calculates 0+725=7250+725=725 cents.

The repair is total += price. Its verified outputs are 1350 cents for the original list, 0 cents for the empty list, and 725 cents for the one-item list. These observations support the repair on the checked cases; they do not by themselves prove correctness for every possible input.

A homework planner sometimes displays the wrong total study time. A classmate sends only a screenshot of the final number. Before changing code, which next action most directly makes the failure reproducible?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A development team keeps a one-page guide that lists the debugging techniques its members use most often. Match each technique in the guide to the description of what it does or produces.

Match each term
A.

Testing the midpoint of a range of commits or inputs so that each check halves the remaining search space

B.

Recording messages about program state to a file or console so behaviour can be reviewed after the run

C.

A variable or formula the debugger re-evaluates and displays every time execution pauses

D.

The chain of active function calls at the moment an error occurred, with a file and line for each frame

E.

Explaining code line by line to a listener, often an inanimate object, until a faulty assumption surfaces

F.

A pause point in a debugger that stops execution only when a stated condition is true

G.

The smallest program and input that still produce the failure, with unrelated code removed

Stack trace

Watch expression

Rubber duck debugging

Conditional breakpoint

Minimal reproducible example

Bisection

Logging

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A school equipment room starts with 50 calculators and records three removals of 6, 9, and 4 calculators. The following Python code should keep track of the number remaining after each removal.


Python
start = 50
remaining = start
for removed in [6, 9, 4]:
    remaining = start - removed


Complete the table with the expected number remaining and the number actually stored in remaining immediately after each loop-body execution. Enter exact integers, measured in calculators.

Removal numberExpected remaining (calculators)Actual remaining (calculators)
1
2
3

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice