Academy Computer Science Producing robust programs

Paper 2 · J277/02 · unit 2.3

Producing robust programs

Defensive design, authentication, validation, maintainable code, testing types and test data. Open exercises and quiz.

Everything on this course is open. Skip, jump, retry. Nothing is locked and there is no required order.

Code that survives bad input, and tests that find bugs on purpose. You can write the Python on the programming page; this unit is the discipline.

2.3.1 Defensive design

Assume the user, the network, and you-last-Tuesday will try to break it.

Anticipate misuse

Empty strings, letters in a number field, huge numbers, SQL-ish quotes, dividing by zero, file missing. Write the nasty cases down first.

Authentication

Prove the user is who they claim: username + password, better with 2FA. Store hashes, not plaintext passwords (the idea is enough at GCSE: you do not keep the real password sitting in a file).

Input validation

Check before you trust the value:

Check Meaning Example
Presence Not empty Name required
Range Between min and max Age 0–120
Type Integer vs string Age is digits
Format Pattern Postcode, email shape
Length Min/max characters Password ≥ 8

Validation is not verification. Validation = “does this look legal?” Verification = “is this the right data?” (type it twice; check a checksum).

Maintainable code

The next human (you in six months) must read it:

  • Sensible names (total_score not x1)
  • Indentation that matches nesting
  • Comments that say why, not “this is a loop”
  • Subprograms so each function does one job

Markers do award “maintainable” in long programming questions. Show it.

2.3.2 Testing

When

  • Iterative testing — as you build, each bit
  • Final / terminal testing — the whole thing against the original requirements

Error types (again, because Paper 2 loves them)

  • Syntax — will not translate
  • Logic — runs, wrong
  • Runtime — crashes mid-flight

Test data

A test plan that only uses “happy” numbers will miss bugs.

Kind Meaning Age 0–120 example
Normal (typical) Valid, ordinary 14
Boundary (extreme valid) On the edge of allowed 0 and 120
Invalid (erroneous) Should be rejected -1, 121, "hello"

Some mark schemes also want extreme as a synonym for boundary. If they list three, use normal / boundary / invalid.

Write a table: test number, data, expected result, actual result, pass/fail. Even if “actual” is blank until you run it — the expected column is the thinking.


Exercises include writing a tiny validation routine and a test plan. Reveal when you want; skip if you already do this in your games.

Practice · optional · answers on this page

Exercises

Do as many or as few as you like, in any order. Hints and a model answer sit under each task.

2.3.1 · e1 python

Validate a score

Score must be an integer 0 to 100 inclusive. Keep asking until it is valid, then print it. Handle non-numeric input without crashing.
Hint
try/except ValueError, or check .isdigit() with care for signs. Range check after int().
Show a model answer
while True: raw = input("Score 0-100: ") try: score = int(raw) except ValueError: print("Must be a whole number") continue if score < 0 or score > 100: print("Out of range") continue break print("Recorded", score)
2.3.1 · e2 paper

Name the check

Presence, range, type, format, or length? 1. Password at least 8 characters 2. Email contains @ 3. Quantity is a number, not "three" 4. Quantity is between 1 and 10 5. Surname is not blank
Hint
Five rows, five names.
Show a model answer
1. Length 2. Format 3. Type 4. Range 5. Presence
2.3.1 · e3 paper

Make it maintainable

Rewrite this so a marker would smile. Keep the same behaviour. x=input("n") y=0 for i in range(int(x)): y=y+i print(y)
Hint
Names, spaces, a comment for the purpose, maybe a function. Note that this sums 0 through n-1.
Show a model answer
# Sum of 0 + 1 + ... + (n-1) n_text = input("How many terms? ") n = int(n_text) total = 0 for number in range(n): total = total + number print(total)
2.3.2 · e4 paper

Test plan for age 11–16

A form only accepts ages 11 to 16 inclusive. Write six tests as (data, kind, expected). Include normal, both boundaries, and invalids (too small, too big, not a number).
Hint
11 and 16 are boundaries. 10 and 17 are invalid. 14 is normal.
Show a model answer
14, normal, accept 11, boundary, accept 16, boundary, accept 10, invalid, reject 17, invalid, reject "twelve", invalid, reject

Check yourself · not a gateway

Quiz

Mark it, reveal it, or skip it. A low score does not close anything. Try again as often as you want.

1 2.3.1 Checking that an input is not blank is…
2 2.3.1 Validation differs from verification because validation…
3 2.3.1 Which help maintainability? (Select all that apply)
4 2.3.2 Test data on the allowed minimum and maximum is called…
5 2.3.2 Iterative testing happens…
6 2.3.2 A program that crashes when you type a letter into an age box has passed robust design.