Mission 1 · Spec 2.3.1
Defensive design and maintainability
Anticipating misuse, authentication, input validation, and writing code that other people can maintain.
- Starter 5 min
- Learn 10 min
- Lab 20 min
- Quiz 10 min
- Exam 10 min
Break it
A program asks "How many tickets (1 to 10)?". List every silly or unexpected thing a user might type.
Reveal
Nothing, 0, -3, 11, 2.5, "two", a million… Defensive design means anticipating all of these so the program doesn't crash or behave wrongly.
Key ideas
Anticipating misuse
Planning for how users might accidentally or deliberately enter unexpected data or try to break the program.
Authentication
Confirming a user's identity, e.g. with a username and password, before giving access.
Input validation
Checking input meets rules before it's used: range, type, length, presence and format checks.
Maintainability
Making code easy for others to read and update: sub programs, sensible naming conventions, indentation and comments.
Validation gatekeeper
Maintainable or not?
Exam-style questions
1. Describe two ways of making a program more maintainable.
[4 marks]Mark scheme
- Comments (1) explain what sections of code do, so others understand it (1)
- Meaningful identifier names (1) make the purpose of variables clear (1)
- Indentation (1) shows the structure of loops and selection (1)
- Sub programs (1) break code into manageable, reusable parts (1)
- Max 4
2. Write an algorithm that asks for a number of tickets and repeats until the input is between 1 and 10 inclusive.
[3 marks]Mark scheme
- Gets input and casts to integer (1)
- Loop that repeats while the number is < 1 or > 10 (1)
- Asks for the input again inside the loop / outputs an error message (1)
TUTOR NOTES
- Misconception: validation checks that data is correct. It only checks it is sensible.
- Misconception: comments make programs run better. They're for humans.
Mission 2 · Spec 2.3.2
Testing
Why we test, iterative and final testing, syntax and logic errors, and choosing normal, boundary, invalid and erroneous test data.
- Starter 5 min
- Learn 10 min
- Lab 20 min
- Quiz 10 min
- Exam 10 min
Testing the edges
A cinema only lets in people aged 15 or over for a 15-rated film. Which ages would you test to be sure the program works?
Reveal
A typical age like 30 (normal); exactly 15 (boundary); 14 (invalid: right type, should be rejected); and "fifteen" (erroneous: wrong type). Most bugs hide at the boundaries.
Key ideas
Purpose of testing
To make sure the program works as intended, finds and removes errors, and handles unexpected input.
Iterative vs final testing
Iterative: testing each part during development, then fixing and retesting. Final (terminal): testing the whole program at the end.
Syntax errors
Break the rules of the language, so the code can't be translated, e.g. a misspelt keyword or a missing bracket.
Logic errors
The program runs but produces an unexpected or wrong result, e.g. using > instead of >=.
| Test data | Meaning | Age 15 to 99 |
|---|---|---|
| Normal | Typical data that should be accepted | 32 |
| Boundary | Data at the edge of what's allowed | 15, 99 |
| Invalid | Correct data type but should be rejected | 14, 120 |
| Erroneous | Wrong data type, should be rejected | "fifteen" |
Choose the test data
A program accepts a percentage from 0 to 100.Bug hunt
This should output the average of 3 marks and "Pass" for 50 or more. Find the three errors.Exam-style questions
1. A program accepts the number of players, from 2 to 6. Complete a test plan with one example each of normal, boundary, invalid and erroneous data, and the expected outcome.
[4 marks]Mark scheme
- Normal: 3, 4 or 5, accepted (1)
- Boundary: 2 or 6, accepted (1)
- Invalid: e.g. 1 or 10, rejected (1)
- Erroneous: e.g. "four", rejected (1)
2. Explain the difference between iterative and final testing.
[2 marks]Mark scheme
- Iterative testing happens during development, testing each part as it's written (1)
- Final / terminal testing happens at the end, testing the whole program (1)
TUTOR NOTES
- OCR distinction: invalid = right type, wrong value; erroneous = wrong type.
- Misconception: a program that crashes while running has a syntax error.