Reducing a Failing Example

When a program fails, the fastest path is often to make the program smaller.

Not smaller in ambition. Smaller in evidence.

A reduced example keeps the failure but removes code that is not needed to produce it.

Start from the failure

Suppose this program fails:

The failure is a TypeError. The code is trying to add an integer and a string.

Remove unrelated data

Keep only enough data to fail:

Now the problem is easier to see. The score is text.

Remove unrelated structure

The dictionary is not the heart of the failure. This is even smaller:

Now the broken assumption is plain:

score should be numeric before addition

A reduced failing example

Runs locally with Python in your browser.

Ready to run.

The rule

Do not change ten things at once.

Change one thing, run again, and compare what happened. If the failure changes, you learned something. If it disappears, you found a relevant part of the program.

Reduction is not the final program

The reduced example is a temporary tool. After you understand the failure, put the fix back into the real program.

For this case, a real fix might be to store numeric scores:

or to convert text at the boundary where the data is read.

Exercise: Choose the smallest failing example

Which reduced example best preserves the failure in this code?

Choose the reduced example

Select one choice, then check.

HintPreserve the failing operation

Keep the addition between an integer and a string, but remove the list and loop.

SolutionKeep only integer-plus-string addition

The direct-addition example still evaluates 0 + "4", so it preserves the TypeError. The other choices either do not add or use two numbers.