Engineering Software Debugging: From Symptom to Verified Fix

Engineering software debugging is the systematic investigation of a program that behaves differently from its specification. You might encounter a crash, an impossible temperature, or a calculation that looks reasonable but uses the wrong units. Your aim is to explain the discrepancy, locate its cause, and gather evidence that a repair works. A successful run alone does not establish a correct calculation.

Establish what should happen

Start with the calculation's contract: its accepted inputs, their units, the intended operation, and the required output. Choose a reference result that does not depend on the suspect code. A hand calculation, a simple limiting case, or a separately derived relationship can provide that reference.


For a scalar numerical result, define the signed discrepancy as actual minus reference:


e=yactual−yreference.e = y_{\text{actual}}-y_{\text{reference}}.


For example, constant speed of 3.00 m/s3.00\ \text{m/s} over 4.00 s4.00\ \text{s} gives a reference distance of 12.0 m12.0\ \text{m}. An output of 0.0120 m0.0120\ \text{m} suggests a scale problem worth investigating; it does not identify the faulty line by itself.


Different symptoms suggest different first checks:


Observed symptom

Failure category

Useful first check

Program cannot be parsed

Syntax error

Inspect the reported statement and nearby syntax

Program stops during execution

Runtime error

Inspect the exception and values at the failure

Program finishes with an incorrect result

Logic or numerical defect

Compare intermediate results with the specification


Reproduce and narrow the failure

A reproducible failure is an unwanted behavior you can obtain again under recorded conditions. Preserve the failing input, relevant settings, program version, expected result, and actual result. If randomness is involved, record the seed when possible.


Reduce the input while retaining the failure. Three readings are easier to trace than three thousand. Check boundaries such as a single reading, a zero load, or the final element of an array, provided these inputs belong to the contract.


Four connected boxes show the stages of a debugging cycle.



Locate the earliest incorrect value

Trace the calculation in execution order. Record selected variable values using temporary output or pause execution to inspect them. Compare each value with an independently calculated expectation, including its units.


The earliest incorrect value is often more informative than the final output. If an area is already wrong immediately after conversion, investigate that conversion before changing the stress formula downstream. If each interval contribution is correct but the running total is wrong, inspect how the total is updated.


Form a specific hypothesis and change one relevant cause at a time. State what observation would support or reject your hypothesis. Several simultaneous edits make that evidence harder to interpret.



Verify the repair

A regression test is a retained check that detects whether previously working behavior fails after a change. Turn the original failure into such a test, then add ordinary cases and relevant boundary cases. Derive expected results independently; copying the program's output into the test preserves its mistakes.


Floating-point comparisons may need an acceptance rule such as


∣yactual−yreference∣≤a+r∣yreference∣,|y_{\text{actual}}-y_{\text{reference}}| \leq a+r|y_{\text{reference}}|,


where aa is an absolute tolerance with the output's units and rr is dimensionless. Select them from the calculation's accuracy requirements and expected numerical error. Do not widen tolerances merely to hide a discrepancy. Passing tests supports correctness for the properties checked; it is not a proof for every possible input.


What to remember

  • Establish an independent expected result and record a repeatable failure.
  • Trace values and units to locate the earliest discrepancy.
  • Test a specific cause, then retain tests that verify the repair.

Example: Debugging an Energy Accumulation Loop

An electrical test rig records power at four times. A Python function estimates transferred energy by treating power as linear between successive readings and adding the trapezoidal interval energies.


Time tt (s)

Power PP (W)

0.0

100

2.0

160

4.0

140

6.0

200


The inputs have equal lengths, contain at least two readings, and have strictly increasing times. The function is:


Python
def energy_j(t_s, power_w):
    energy = 0.0
    for i in range(1, len(t_s)):
        energy = (
            0.5 * (power_w[i - 1] + power_w[i])
            * (t_s[i] - t_s[i - 1])
        )
    return energy


a) Calculate the intended energy and trace the value stored in energy after each iteration. Where does the running total first become incorrect?


For each interval, multiply the average endpoint power by the elapsed time. Because 1 W=1 J/s1\ \text{W}=1\ \text{J/s}, each contribution has units of joules:


ΔE1=100+1602 W×(2.0−0.0) s=260 J,ΔE2=160+1402 W×(4.0−2.0) s=300 J,ΔE3=140+2002 W×(6.0−4.0) s=340 J.\begin{aligned} \Delta E_1&=\frac{100+160}{2}\ \text{W}\times(2.0-0.0)\ \text{s}=260\ \text{J},\\ \Delta E_2&=\frac{160+140}{2}\ \text{W}\times(4.0-2.0)\ \text{s}=300\ \text{J},\\ \Delta E_3&=\frac{140+200}{2}\ \text{W}\times(6.0-4.0)\ \text{s}=340\ \text{J}. \end{aligned}


The intended accumulated energy is


E=(260+300+340) J=900 J.E=(260+300+340)\ \text{J}=900\ \text{J}.


The assignment inside the loop replaces the stored value on every iteration:

  • After the first interval, the stored value is 260 J260\ \text{J}, matching the intended running total of 260 J260\ \text{J}.
  • After the second interval, the stored value is 300 J300\ \text{J}, but the intended running total is 260+300=560 J260+300=560\ \text{J}.
  • After the third interval, the stored value is 340 J340\ \text{J}, but the intended running total is 560+340=900 J560+340=900\ \text{J}.

The first incorrect running total occurs after the second interval. Each interval contribution is correct, but the function returns only the last contribution, 340 J.


b) Make the smallest relevant repair and trace the corrected running total.


The program needs to add each interval contribution to the energy already stored. Inside the loop, replace energy = ( with energy += (. Keep the initialization energy = 0.0, the interval-energy expression and the return statement unchanged. In Python, this accumulation update adds the calculated contribution to the current value of energy.


Starting from ________, the repaired updates are


E1=(0+260) J=260 J,E2=(260+300) J=560 J,E3=(560+340) J=900 J.\begin{aligned} E_1&=(0+260)\ \text{J}=260\ \text{J},\\ E_2&=(260+300)\ \text{J}=560\ \text{J},\\ E_3&=(560+340)\ \text{J}=900\ \text{J}. \end{aligned}


The corrected function returns 900 J. This repair fixes the update responsible for the earliest incorrect running total without changing the valid interval-energy formula.


c) Verify the repair using a separate constant-power case: t_s = [0.0, 1.5, 3.0] and power_w = [120.0, 120.0, 120.0]. Explain why this case is more informative about accumulation than a case with only one interval.


For constant power, calculate the expected result independently using total elapsed time:


E=PΔt=(120 W)(3.0 s−0.0 s)=360 J.E=P\Delta t=(120\ \text{W})(3.0\ \text{s}-0.0\ \text{s})=360\ \text{J}.


Each of the ________ intervals lasts 1.5 s1.5\ \text{s} and contributes


ΔE=(120 W)(1.5 s)=180 J.\Delta E=(120\ \text{W})(1.5\ \text{s})=180\ \text{J}.


The repaired code accumulates 180+180=360 J180+180=360\ \text{J}. The original code overwrites the first contribution and returns 180 J180\ \text{J}, so this test detects the accumulation defect.


With only one interval, assignment and accumulation from zero produce the same value. Such a test would check an interval calculation but would not expose this defect. Retain both the original case expecting 900 J and this separate case expecting 360 J as regression tests.

A stress calculation is specified to use σ=F/A\sigma=F/A, with force F=840.0 NF=840.0\ \text{N} and area A=0.00028 m2A=0.00028\ \text{m}^2. The following Python code runs to completion without an exception:


Python
force_n = 840.0
area_m2 = 0.00028
stress_pa = force_n * area_m2
print(stress_pa)


It prints 0.2352, although the reference stress is 3.00×106 Pa3.00\times10^6\ \text{Pa}. Which diagnosis best describes the demonstrated failure?

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

You are investigating an engineering calculation that sometimes fails during execution. Match each debugging aid to its specific function so that you can choose the appropriate aid for the evidence you need.

Match each term
A.

Displays the current value of a selected calculation or variable as you step through execution.

B.

Reports the sequence of active function calls leading to the location of a failure.

C.

Pauses execution at a selected location so that you can inspect the program before continuing.

D.

Checks a condition during execution and signals a failure if that condition is false.

Stack trace

Breakpoint

Watch expression

Assertion

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 torque program has two functions. The outer function accepts force in newtons and radius in millimetres, converts the radius to metres, and calls a core function specified to compute T=FrT=Fr using metres.


For F=30.0 NF=30.0\ \text{N} and radius 200 mm200\ \text{mm}, the outer function repeatedly returns 6000 N⋅m6000\ \text{N}\cdot\text{m} instead of 6.00 N⋅m6.00\ \text{N}\cdot\text{m}. You suspect the radius conversion is missing, but have not inspected the intermediate values. Which next action most directly tests that hypothesis?

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

Extra Practice