GCSE Computer Science (OCR)

Topic Summaries

ALGORITHMS AND PROGRAMMING: Producing robust programs

Defensive design

  • Defensive design is about writing programs that can anticipate problems before they happen – especially incorrect inputs or misuse.
  • Anticipating misuse: users might enter invalid, unexpected, or malicious input. The program should handle this without crashing. 
    • Examples of misuse include typing letters into a number input, leaving fields blank, and entering a script or code (e.g. for hacking)
    • This can be addressed by using input validation, using authentication (to check who the user is), or displaying error messages and ask for correct input
  • Authentication: is used to verify a user’s identity, typically using:
    • Username + Password
    • Sometimes 2FA (two-factor authentication)
  • Input validation: checks that the user’s input is acceptable before using it in the program. Validation prevents bugs and improves security.
Common types of checks What it does Example
Length Checks input is not too short or long Password length > = 8
Range Ensures number is within limits Age between 0 and 120
Type Checks if input is the right type Number not string
Format Pattern is correct Email contains @
Presence Ensures input is not empty Name is not blank

# Authentication example

username = input(“Enter username:”)

password = input(“Enter password:”)

 
 

if username == “admin

and password == “1234

print(“Access granted”)

else:

print(“Access denied”)

# Age validation example

age = input(“Enter your age:”)

 
 

if age.isdigit():

age = int(age)

if age > = 0 and age < = 120:

print(“Valid age”)

else:

print(“Age out of range”)

else:

print(“Please enter a number”)

Unlock Defensive design

Sign up to unlock the rest of this topic — plus every other topic, video, flashcard set and eBook guide in your SnapRevise Digital subscription.

Cancel anytime — instant access the moment you sign up.