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:
For example, constant speed of 3.00 m/s over 4.00 s gives a reference distance of 12.0 m. An output of 0.0120 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.
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
where a is an absolute tolerance with the output's units and r 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 t (s) | Power P (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:
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 energya) 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/s, each contribution has units of joules:
The intended accumulated energy is
The assignment inside the loop replaces the stored value on every iteration:
- After the first interval, the stored value is 260 J, matching the intended running total of 260 J.
- After the second interval, the stored value is 300 J, but the intended running total is 260+300=560 J.
- After the third interval, the stored value is 340 J, but the intended running total is 560+340=900 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
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:
Each of the ________ intervals lasts 1.5 s and contributes
The repaired code accumulates 180+180=360 J. The original code overwrites the first contribution and returns 180 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.
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.
Displays the current value of a selected calculation or variable as you step through execution.
Reports the sequence of active function calls leading to the location of a failure.
Pauses execution at a selected location so that you can inspect the program before continuing.
Checks a condition during execution and signals a failure if that condition is false.
Stack trace
Breakpoint
Watch expression
Assertion
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=Fr using metres.
For F=30.0 N and radius 200 mm, the outer function repeatedly returns 6000 N⋅m instead of 6.00 N⋅m. You suspect the radius conversion is missing, but have not inspected the intermediate values. Which next action most directly tests that hypothesis?