What is defensive design?
Planning ahead so a program still works when users get things wrong.
What if the user gets it wrong?
Key facts
| Method | What it does | Example |
|---|---|---|
| Anticipating misuse | Stops the user breaking or hacking the program | Input sanitisation: removes special characters such as quotation marks and semicolons |
| Input validation | Checks the data entered is sensible | Range check, presence check, type check, length check |
| Authentication | Confirms who the user is | Username and password, two-factor authentication (2FA) |
| Maintainability | Makes the code easy for other programmers to understand | Comments, meaningful variable names |
What else could go wrong?
ActivitySort it: which method?
ActivityWhich method would help?
Input validation
Checking the data is sensible before the program uses it.
Types of validation checks
Key facts
| Check | It checks that… | Example |
|---|---|---|
| Presence | Some data has been entered | The name box is not blank |
| Type | The data is the correct data type | A whole number, not letters |
| Range | A number is within set limits | A mark from 0 to 100 |
| Length | It has the right number of characters | A password of at least 8 |
| Lookup | It is one of a list of allowed values | A size that must be S, M or L |
| Format | It follows a set pattern | A date like DD/MM/YYYY |
ActivitySort it: which validation check?
ActivityFill the gaps: which check?
Authentication
Confirming who the user is.
Logging in
Key facts
| Way to authenticate | How it confirms who you are |
|---|---|
| Username and password | The password must match the username |
| Biometrics (fingerprint, face scan) | Uses a feature of the user’s body to confirm who they are |
| Two-factor authentication (2FA) | Needs two different proofs, e.g. a password AND a code sent to the user’s phone |
| CAPTCHA (“I am not a robot”) | Checks the user is a real person, not a bot |
Don’t mix up: authentication checks WHO the user is. Validation checks the DATA is sensible.
ActivitySort it: authentication or validation?
ActivityPut the login steps in order
Maintainability
Code that other programmers can understand and update.
Hard or easy to maintain?
Key facts
| Feature | How it helps |
|---|---|
| Sub programs (functions and procedures) | Split the program into smaller parts that can be reused, tested and fixed |
| Naming conventions | Meaningful names show what a variable stores |
| Indentation | Shows which lines belong together (loops, if statements) |
| Commenting | Explains what the code does, so others can follow it |
Also credited: a constant (e.g. VAT = 0.2) means a value only has to be changed in one place.
ActivitySort it: which maintainability feature?
ActivityImprove the code
ActivityFill the gaps
Exam questions
Type your answer, then mark it against the mark scheme.
Challenges
Test yourself! Answer 10 questions correctly to finish this step.