Skip to main content
EnglandComputer ScienceSyllabus dot point

How do you design a program that copes with unexpected input and is easy to maintain?

Defensive design: anticipating misuse, input validation and sanitisation, authentication, and writing maintainable programs through comments, indentation and sensible naming.

An OCR J277 2.3.1 answer on defensive design: anticipating misuse, validating and sanitising input, authentication, and writing maintainable programs with comments, indentation and sensible variable names.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Anticipating misuse
  3. Input validation
  4. Authentication
  5. Maintainability
  6. Try this

What this dot point is asking

OCR wants you to explain defensive design: writing programs that anticipate misuse, validate and sanitise input, use authentication, and are maintainable through comments, indentation and sensible naming. The goal is a robust program that does not crash or misbehave when given unexpected input, and that another programmer can understand. This is examined in Paper 2, often alongside testing.

Anticipating misuse

Input validation

A re-prompt loop is the standard pattern:

score = int(input("Enter a score 0 to 100: "))
while score < 0 OR score > 100
  print("Out of range, please re-enter")
  score = int(input("Enter a score 0 to 100: "))
endwhile

Authentication

Maintainability

Try this

Q1. State what input validation is. [1 mark]

  • Cue. Checking that data entered is reasonable and acceptable before the program uses it.

Q2. Name two validation checks and what each tests. [2 marks]

  • Cue. Any two: range check (value within limits), type check (correct data type), length check (acceptable number of characters), presence check (not blank), format check (matches a pattern).

Q3. State one technique that makes a program easier to maintain. [1 mark]

  • Cue. Any one: comments explaining the code, consistent indentation, sensible meaningful names, or using subprograms.

Exam-style practice questions

Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

OCR 20214 marksA program asks the user to enter their age, which must be a whole number between 0 and 120. Describe how input validation could be used here, and write an algorithm that keeps asking until a valid age is entered.
Show worked answer →

Validation here is a range check (and a check that the input is a whole number): reject anything below 0 or above 120, and re-prompt.

age = int(input("Enter your age: "))
while age < 0 OR age > 120
  print("Invalid, try again")
  age = int(input("Enter your age: "))
endwhile

Marks: a suitable validation check (range 0 to 120) (1), a loop that repeats while the input is invalid (1), re-prompting inside the loop (1), and correct logic so a valid value exits the loop (1). Markers reward a working re-prompt loop; checking the value only once, without looping, does not keep asking.

OCR 20224 marksExplain two techniques that make a program easier to maintain, and explain why anticipating misuse is part of defensive design.
Show worked answer →

Two maintainability techniques (1 mark each): adding comments to explain what sections of code do, so another programmer can understand it; using sensible, meaningful variable and subprogram names so the code is self-explanatory; using consistent indentation so the structure (loops, selection) is clear; and using subprograms to break the code into named parts.

Anticipating misuse (up to 2): users will enter unexpected or invalid data (wrong type, out of range, blank), whether by mistake or deliberately, so a robust program must predict this and handle it (with validation) rather than crash or behave incorrectly.

Markers reward two genuinely different maintainability techniques and a clear reason that anticipating misuse prevents crashes and incorrect behaviour from unexpected input.

Related dot points

Sources & how we know this