Algorithm-to-Program Development · Lesson 3 of 3
Testing and Refinement
Debugging finds and corrects defects. Distinguish syntax errors that prevent interpretation from logical errors that produce incorrect behaviour. Reproduce a failure with a specific input, inspect state, make a focused correction and rerun relevant tests.
By the end, you can
- Test programs systematically.
- Identify logical errors.
- Correct errors.
- Retest after modifications.
- Evaluate whether the final solution meets requirements.
Debugging
Three kinds of error appear, and they are found in different ways. A syntax error breaks the rules of the language, so the program will not translate at all, and the translator names the line. A run-time error stops a running program — dividing by zero, reading past the end of an array, opening a file that is not there. A logic error breaks nothing: the program runs, finishes, and gives the wrong answer.
Logic errors are the ones that need method. Reproduce the fault with the smallest input that shows it. Work out what the value of each variable should be at each step, then find out what it actually is — with a trace table, or by printing values temporarily, or with a debugger stepping line by line. The first step where the actual differs from the expected is where the fault is, and it is often several lines before the symptom.
The usual causes are worth checking first: a variable not initialised, a total not reset, a loop that runs one time too many or too few, > written where >= belongs, the branches of an IF the wrong way round, and integer division silently discarding a fraction.
Retest after every fix, and retest everything rather than only the case that failed. A change that fixes one case commonly breaks another, which is why the original test plan is run again in full.
Evaluating the finished solution means going back to the requirement and checking it point by point: does it produce every output that was asked for, handle invalid input without crashing, work at the boundaries, and behave sensibly on an empty list or a single item? Honest evaluation names what does not work as well as what does, and says what would be improved with more time.
Worth remembering
- Syntax errors stop translation, run-time errors stop execution, logic errors do neither.
- Find a logic error by comparing expected with actual values step by step.
- Retest the whole plan after a fix, not only the case that failed.
Worked example
If a pass threshold of 50 rejects 50, inspect whether the condition incorrectly uses > instead of >=.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
Check 49, 50 and 51, expecting fail, pass and pass, plus other required validation cases.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.