Program design, testing and maintenance
A-Level Computer Science · Algorithms and Data Structures
Decomposition and modularity
Break a problem into subproblems, each solved by a procedure (no return value) or function (returns a value). Parameters may be passed by value — the routine gets a copy and the original is unchanged — or by reference, where the routine can change the caller's variable. Local variables exist only inside the routine, which is what makes modules independent and reusable.
Structured English and pseudocode
Use IF … THEN … ELSE … ENDIF, FOR … NEXT, WHILE … ENDWHILE, REPEAT … UNTIL. A WHILE loop may run zero times because the condition is tested first; a REPEAT loop always runs at least once. Choosing the wrong one is a standard exam trap.
Test data, in three kinds
Normal data should be accepted; abnormal (erroneous) data should be rejected; boundary data sits exactly on the edge of the acceptable range and is where most defects hide. For a value that must be $1$–$100$, the boundary tests are $0$, $1$, $100$ and $101$ — and giving both sides of each edge is what earns the marks.
Kinds of testing
Unit or module testing checks one routine; integration testing checks they work together; alpha testing is in-house; beta testing uses real users; acceptance testing checks the requirements are met.
Maintenance
Corrective fixes faults found after release; adaptive changes the program for a new environment or new law; perfective improves performance or usability. Most of a system's lifetime cost is maintenance, not writing it — which is the real argument for clear naming, comments and modular design.
Worked example: choosing a loop and designing test data
Write pseudocode for a program that asks the user to enter a score from $0$ to $100$, repeating until the input is valid, then prints the grade: $\ge80$ Excellent, $\ge65$ Good, $\ge50$ Pass, otherwise Fail.
Then build a table of test data.
Solution
Pseudocode
REPEAT
OUTPUT "Enter score (0-100): "
INPUT Diem
IF Diem < 0 OR Diem > 100 THEN
OUTPUT "Invalid score"
ENDIF
UNTIL Diem >= 0 AND Diem <= 100
IF Diem >= 80 THEN
OUTPUT "Excellent"
ELSE IF Diem >= 65 THEN
OUTPUT "Good"
ELSE IF Diem >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Why use REPEAT and not WHILE: input must be taken at least once before there is anything to check. Using WHILE here would require initializing Diem with a dummy value before the loop, which is longer and more error-prone.
Why the IF order must go from HIGH to LOW: if you tested $\ge50$ first, a score of $90$ would fall into the "Pass" branch and never reach the "Excellent" branch.
Table of test data
Normal: $72$ ⇒ Good; $55$ ⇒ Pass; $91$ ⇒ Excellent. (Choose values in the middle of each range to test each branch.)
Abnormal: $-5$ ⇒ error message and ask again; $150$ ⇒ error message and ask again; "abc" ⇒ must be rejected (data type check).
Boundary: the most important, and must come in PAIRS:
$-1$ and $0$ (lower edge of the valid range)
$100$ and $101$ (upper edge)
$49$ and $50$ (Fail / Pass edge)
$64$ and $65$ (Pass / Good edge)
$79$ and $80$ (Good / Excellent edge)
Why they must come in pairs: testing only $80$ will not reveal that the programmer wrote > instead of >=. Only the pair $79$ and $80$ can tell the two cases apart, and this is exactly the kind of error boundary testing exists to catch.
Common mistake
Giving only ONE value per boundary. Boundary testing must give a value just inside and just outside the edge ($49$ and $50$, $100$ and $101$), because the most common error here is confusing> with >=, and a single value cannot detect it.Key takeaway
WHILE checks the condition first so it can run $0$ times; REPEAT always runs at least once. Test data must cover all three types, and boundaries must come in pairs. Remember the three types of maintenance, and remember that maintenance accounts for most of the lifecycle cost.