26-defensive-design-validation.py
python-programming / robust-programming  ·  Lesson 27
Lesson 27 — Robust programming

Defensive Design & Validation

Anticipate misuse and reject invalid values before processing them.

Defensive design considers what could go wrong and protects the program against likely misuse. Validation checks whether input is acceptable before it is used.

Predict

Read the code first. Do not run it yet.

defensive-design-validation-predict.py
choice = input("Y or N: ")
while choice != "Y" and choice != "N":
    choice = input("Y or N: ")
print("Accepted")
Predict before running
  • Which values are accepted?
  • Which condition causes the loop to repeat?
  • What kind of misuse is being anticipated?

Investigate

Use this code to explain exactly how the program works.

defensive-design-validation-investigate.py
age = int(input("Age 11-16: "))
while age < 11 or age > 16:
    age = int(input("Age 11-16: "))
print("Valid age")
Investigate the code
  • What makes a value invalid?
  • Why is or correct in the loop condition?
  • What happens with ages 11 and 16?

Validation

validation.py
rating = int(input("Rating 1-5: "))
while rating < 1 or rating > 5:
    print("Invalid rating")
    rating = int(input("Rating 1-5: "))
print("Thank you")
How it works
  • The program checks the allowed range.
  • Invalid values trigger another input request.
  • Boundary values 1 and 5 are valid.
Validation is not verificationValidation checks whether data is sensible and acceptable. It does not prove that the data is factually correct.

Self-study guide

Defensive design means anticipating how a program might be misused or given unsuitable data and designing it to cope sensibly. OCR specifically includes anticipating misuse, authentication, input validation and maintainability considerations.

Validation checks whether input satisfies rules. It does not prove that the data is factually correct; it only checks that the data is acceptable according to the program’s criteria.

OCR focusUnderstand how to cater for likely input values and invalid data, and gain practical experience designing input validation. Keep the focus on validation rules and defensive thinking.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
input validationwhile/selection checks in PythonReject data that breaks stated rules.
range checkvalue < minimum or value > maximumChecks numerical boundaries.
length checklen(text)Checks the number of characters.
anticipating misusedefensive conditions and limitsThink about likely incorrect or hostile input.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

A validation loop keeps requesting data until the rule is satisfied.

lesson-27-worked.py
mark = int(input("Mark 0-100: "))

while mark < 0 or mark > 100:
    print("Invalid mark")
    mark = int(input("Mark 0-100: "))

print("Accepted", mark)
Output
Accepted 72
Work through it line by line
  • The first input is checked immediately.
  • Values below 0 or above 100 are invalid.
  • The loop repeats only while the value breaks the rule.
  • Boundary values 0 and 100 are valid and therefore accepted.

Another example

A string rule can be validated in the same way.

lesson-27-example-2.py
username = input("Username: ")

while len(username) < 4 or len(username) > 12:
    username = input("Username: ")

print("Username accepted")
Output
Username accepted
Why this example matters
  • The rule concerns length rather than numerical value.
  • The program anticipates inputs that are too short or too long.

Common mistakes

Avoid these errors
  • Calling validation “verification”. Verification is a different idea; OCR’s required defensive-design list here focuses on validation.
  • Using < instead of <= carelessly at boundaries.
  • Validating after the value has already been used in an important calculation.
  • Checking only one side of a range.
Exam-safe warningValidation checks acceptability against rules. It does not guarantee that an accepted value is truthful or factually correct.

Exam-ready checklist

You should now be able to
  • Explain defensive design and anticipating misuse.
  • Design range and length validation.
  • Choose correct boundary conditions.
  • Explain how invalid data should be handled.

What you have learned

  • Defensive design anticipates likely misuse.
  • Validation checks input against rules.
  • A loop can repeatedly request input until it is valid.
  • Boundary values need careful consideration.