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.
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 misuse | How the program defends against it |
|---|---|
| The user types letters where a number is needed | Check the type of the input and ask again |
| The user leaves a box empty | Check that something was entered |
| The user enters 250 as their age | Check that the number is in a sensible range |
| Someone keeps guessing a password | Allow only a few attempts, then lock the account |
| Someone who should not see the data tries to log in | Make users prove who they are |
Authentication
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.
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
Input validation
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.
| Check | What it tests | Example |
|---|---|---|
| Range check | The value lies between a lowest and a highest value | Age must be 11 to 18 |
| Length check | The data has an allowed number of characters | A password has at least 8 characters |
| Presence check | Something has been entered | The name box is not empty |
| Format check | The data follows a set pattern | A date looks like 25/12/2024 |
| Type check | The data is the right data type | A 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.
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
password = input("Choose a password")
while password.length < 8
print("Use at least 8 characters")
password = input("Choose a password")
endwhile
Presence check
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.
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.
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.
Maintainability
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.
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.
// 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))
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.