Skip to content
IGCSE·Tuition
Computer Science · Lessons

Prepare a minimal reproducible learning example

A long program that fails is hard to explain, and a teacher or a classmate cannot help until it is smaller.

On this page
  1. How do you shrink a failing program?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

A minimal reproducible example is the smallest code and data that still produce the wrong result, with the expected and actual output written beside it. You use it to find a fault yourself, to explain it in writing or to ask a teacher for help.

This lesson completes the problem-solving explanations module. It uses describing a correction and connecting a trace with output once the example is small enough to trace by hand.

How do you shrink a failing program?

  1. Write the symptom. Note the input, the expected output and the actual output.
  2. Replace input with fixed values. Remove INPUT and assign known data instead.
  3. Delete one unrelated part at a time. Remove the other calculations, messages and menus.
  4. Run again after each deletion. If the wrong output remains, the deletion was safe. If it changes or vanishes, restore the line.
  5. Reduce the data. Use the fewest values that still fail, with numbers that are easy to calculate.
  6. State expected and actual output with the one line you suspect.

Worked example

A longer program reads scores into an array, then finds the total, the average and the highest score. The total is wrong. The loop is:

Total ← 0
FOR Index ← 2 TO 5
   Total ← Total + Scores[Index]
NEXT Index

Step 1, symptom. For five scores, the total is smaller than expected.

Step 2, fixed data. Remove the input and use three scores: Scores[1] ← 10, Scores[2] ← 20, Scores[3] ← 30.

Step 3, delete unrelated parts. The average and highest score do not touch Total, so remove them. Change the loop limit to 3 to match the data.

Minimal example:

Scores[1] ← 10
Scores[2] ← 20
Scores[3] ← 30
Total ← 0
FOR Index ← 2 TO 3
   Total ← Total + Scores[Index]
NEXT Index
OUTPUT Total

Expected output: 60 (10 + 20 + 30). Actual output: 50 (20 + 30).

Why it points to the fault. The loop starts at 2, so Scores[1] is never added. The correction is to start the loop at 1: FOR Index ← 1 TO 3.

Check. The example still fails after every deletion, and it is short enough to trace in four steps.

The mistake to watch for

A frequent slip is to shrink too far or to change the problem:

Scores[1] ← 10 and Total ← 0 with no loop. Output: 0.

The loop that holds the fault has been deleted, so the example no longer shows the problem. Another slip is to submit the example without the expected output, so nobody can tell what “wrong” means.

The correction: after every deletion, rerun the example and check that the actual output is still wrong. Always write expected and actual output together.

Check yourself

1. For the Total fault above, which parts must stay in a minimal example: (a) the input of names, (b) the loop, (c) the highest-score calculation, (d) the array values?

Show answer

Keep (b) the loop and (d) the array values. The loop holds the fault and the values give a result that can be checked. The input of names and the highest-score calculation do not affect Total, so they can be removed.

2. Reduce the example to two scores, 10 and 20, with the same faulty loop FOR Index ← 2 TO 2. Does it still show the fault? State expected and actual.

Show answer

Yes. Expected total is 30 (10 + 20). The loop runs once, for Index = 2, so only Scores[2] = 20 is added. Actual total is 20. The fault remains, and the example is even easier to trace.

3. A student says a program that counts values above 10 gives 2 for the data 5, 10, 15 but should give 1. Write the minimal example’s expected and actual output, and name the likely fault.

Show answer

Expected: 1, because only 15 is above 10. Actual: 2, because the code counts 10 and 15. The likely fault is a comparison >= 10 where > 10 is needed. The data 5, 10, 15 is a good minimal set because 10 sits exactly on the boundary.

Where this leads next

Now bring the five skills together in the problem-solving explanations practice set. To keep track of repeated errors, the mistake log and retest queue helps you note the fault type and set a retest.

If you want someone to shrink a stubborn program with you and talk through your reasoning, our teachers can do that in online one-to-one Computer Science tuition.

Questions people ask

What is a minimal reproducible example?

It is the smallest piece of code and data that still shows the same fault, together with the expected and actual output. Anyone can run it, see the problem and start fixing it without reading the rest of your program.

How small should the example be?

Small enough that you can trace it by hand in a minute, but not so small that the fault disappears. After every deletion, run it again. If the wrong output is still there, keep the deletion. If it vanishes, put the line back.

Do I need to know the fault before I shrink the program?

No. Shrinking is a way of finding the fault. Each removed line that does not change the result is not the cause, so the lines that remain are the suspects. Often the cause becomes obvious once only a few lines are left.

Sources

  1. Cambridge IGCSE Computer Science 0478 syllabus page

Updated:

Your next step

If your programs fail and you cannot yet say why, a one-to-one teacher can shrink the problem with you and show how a small example points straight at the fault.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service