Skip to content

2.3.1 Defensive design

GCSE · Paper 2 · Computational thinking, algorithms and programming · Section 2.3 · 15 flashcards · 9 practice questions

Questions

2.3.1 Defensive design

A good program does not only work when everything goes right. It also copes when people make mistakes or try to cause trouble. Writing a program in this way is called defensive design. It also means writing the code so that other people can understand and change it later. All code here is in the OCR Exam Reference Language.

Definition
Defensive design means planning a program so that it keeps working correctly, and stays secure, when it is used in unexpected ways.

Anticipating misuse

To anticipate something means to think about it before it happens. A programmer asks, "What could go wrong?" and then writes code to deal with it. Misuse can be an accident or on purpose.

Possible misuseHow the program defends against it
The user types letters where a number is neededCheck the type of the input and ask again
The user leaves a box emptyCheck that something was entered
The user enters 250 as their ageCheck that the number is in a sensible range
Someone keeps guessing a passwordAllow only a few attempts, then lock the account
Someone who should not see the data tries to log inMake users prove who they are
Key Point
Never trust input. Assume that some users will make mistakes and that a few will try to break the program.

Authentication

Definition
Authentication is confirming that a user is who they say they are, for example by asking for a username and a password.

The username says who the user claims to be. The password is secret and proves it. The program compares both with the stored details. Access is allowed only if they match. This stops strangers from using someone else's account.

You can make authentication stronger by limiting the number of attempts. This program gives the user three tries.

pseudocode
attempts = 0
loggedIn = false
while attempts < 3 AND loggedIn == false
    username = input("Username")
    password = input("Password")
    if username == "maya" AND password == "Tiger42" then
        loggedIn = true
    else
        attempts = attempts + 1
    endif
endwhile
if loggedIn == true then
    print("Welcome")
else
    print("Account locked")
endif
Warning
A real system would not keep a password written in the program, and it would never show it on the screen. The code above is only to show the idea. Passwords are case sensitive, so "tiger42" does not match "Tiger42".

Input validation

Definition
Input validation is a check made by the program that data entered is sensible and allowed before it is used.

Validation does not prove that data is correct. A person who is 16 could type 17. It is sensible and in range, so validation accepts it. Validation only rejects data that cannot be right.

CheckWhat it testsExample
Range checkThe value lies between a lowest and a highest valueAge must be 11 to 18
Length checkThe data has an allowed number of charactersA password has at least 8 characters
Presence checkSomething has been enteredThe name box is not empty
Format checkThe data follows a set patternA date looks like 25/12/2024
Type checkThe data is the right data typeA quantity is a whole number

Range check

The loop keeps asking until the value is in range. The condition uses OR because a value is bad if it is too low or too high.

pseudocode
age = int(input("Enter your age (11 to 18)"))
while age < 11 OR age > 18
    print("Age must be from 11 to 18")
    age = int(input("Enter your age (11 to 18)"))
endwhile

Length check

pseudocode
password = input("Choose a password")
while password.length < 8
    print("Use at least 8 characters")
    password = input("Choose a password")
endwhile

Presence check

pseudocode
name = input("Enter your name")
while name == ""
    print("You must enter a name")
    name = input("Enter your name")
endwhile

Format check

This check makes sure a date has ten characters with a "/" at positions 2 and 5. It does not check that the day or month is real.

pseudocode
date = input("Enter a date as dd/mm/yyyy")
if date.length == 10 AND date.substring(2, 1) == "/" AND date.substring(5, 1) == "/" then
    print("Format accepted")
else
    print("Wrong format")
endif

Type check

A type check makes sure the data is the right type, such as a whole number. The language has no single command for this, so a program looks at each character. The character codes for the digits 0 to 9 are 48 to 57. You would be given those numbers in an exam.

pseudocode
quantity = input("How many?")
allDigits = true
for i = 0 to quantity.length - 1
    code = ASC(quantity.substring(i, 1))
    if code < 48 OR code > 57 then
        allDigits = false
    endif
next i

If allDigits is still true at the end, every character was a digit. An empty string also passes this loop, so combine a type check with a presence check.

Exam Tip
Name the check and say what it tests. "Range check: the number must be between 1 and 10" earns the mark. "It checks the input" does not.

Maintainability

Definition
A program is maintainable when other programmers, or you in a year's time, can easily read it, fix it and change it.

Programs are used for years, so they are changed many times. Four habits make them easier to maintain.

* Sub programs. Split a long program into functions and procedures that each do one job. Each can be tested on its own and reused without copying code.
* Naming conventions. Choose meaningful, consistent names. totalPrice tells the reader more than x. Use the same style for every name, such as starting with a lower-case letter.
* Indentation. Move the code inside an if, a loop or a sub program to the right. The shape of the code then shows its structure.
* Commenting. Write notes after // to explain what a section does and why. The computer ignores them.

Before and after

This program works but is hard to follow.

pseudocode
a = int(input("Enter"))
b = int(input("Enter"))
if a > b then
print(a)
else
print(b)
endif

This version does the same job and is easier to maintain.

pseudocode
// Return the larger of two numbers
function largest(first, second)
    if first > second then
        return first
    else
        return second
    endif
endfunction

firstNumber = int(input("Enter the first number"))
secondNumber = int(input("Enter the second number"))
print(largest(firstNumber, secondNumber))
Remember
Maintainability has four parts: sub programs, naming, indentation and comments. Each one makes the code easier to read and change.

Exam technique

* State or give: one short point, such as "A range check tests that a value is between two limits."
* Describe: say what the thing is or does. For a validation check, say what it tests and give a small example.
* Explain: give a reason as well. For example, comments help because another programmer can see what a section is for without working it out.
* Write or complete a validation routine: use a loop that repeats until the input is valid, in the reference language. Check that your condition covers both ends of the range.
* Improve a program: rename variables, indent the inside of every if and loop, add comments, and move repeated or separate jobs into sub programs. Name each change you made.