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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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
- The purpose of testing, the difference between iterative and terminal (final) testing, and the types of test data (normal, boundary and erroneous or invalid), with how to choose suitable test data.
An OCR J277 2.3.2 answer on testing: the purpose of testing, iterative versus terminal (final) testing, and the three types of test data (normal, boundary and erroneous or invalid), with how to choose suitable test data and build a test plan.
- The two main types of programming error: syntax errors and logic errors, what causes each, how they are found, and how they differ.
An OCR J277 2.3.2 answer on the two main types of programming error: syntax errors (breaking the rules of the language) and logic errors (the program runs but gives the wrong result), what causes each, and how they are found and corrected.
- The three basic programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), and when to use each.
An OCR J277 2.2.2 answer on the three programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), with the OCR Exam Reference Language for each.
- The use of variables and constants, the common data types (integer, real, Boolean, character and string), choosing an appropriate data type, and casting (converting) between data types.
An OCR J277 2.2.1 answer on variables and constants, the common data types (integer, real, Boolean, character, string), choosing an appropriate data type for data, and casting between data types.
- Methods to identify and prevent vulnerabilities: penetration testing, anti-malware software, firewalls, user access levels, passwords, encryption, physical security and network policies.
An OCR J277 1.4.2 answer on the methods used to identify and prevent vulnerabilities: penetration testing, anti-malware software, firewalls, user access levels, passwords, encryption, physical security and network policies.
Sources & how we know this
- OCR GCSE (9-1) Computer Science (J277) specification — OCR (2020)