Defensive design
- Anticipate misuse: think about the mistakes and mischief users might try, and plan for them.
- Authentication checks that users are who they say they are, for example with a username and password.
- Validation checks that data is sensible before it is used: a range check (between 1 and 100), a type check (a number), a length check (at least 8 characters), a presence check (not left blank) or a format check (matches a pattern, such as a postcode).
- Verification checks that data was entered correctly, for example by typing a new password twice.
Maintainability
- Make code easy to understand and update: use comments, meaningful names, indentation, subroutines and constants.
Errors
- A syntax error breaks the rules of the language, such as a missing bracket, so the program won't run.
- A logic error lets the program run, but it gives the wrong result, such as using < instead of <=.
- A runtime error happens while the program runs, such as dividing by zero.
Testing
- Iterative testing happens during development, as each part is written. Final (terminal) testing happens once the program is complete.
- Test data: normal data (typical values that should be accepted), boundary data (values at the edge of the valid range, and just outside it) and erroneous or invalid data (values that should be rejected).
- For valid ages 11 to 18: normal 14; boundary 11 and 18 (and 10 and 19 just outside); erroneous 'ten' or −5.
- A test plan lists each test, its test data, the expected result and the actual result.
Key terms
- Validation
- Checking that data is sensible and allowed before it is used.
- Verification
- Checking that data has been entered correctly, such as by typing it twice.
- Authentication
- Checking that a user is who they claim to be.
- Syntax error
- An error that breaks the rules of the language, so the code won't run.
- Logic error
- An error that lets the program run but gives the wrong result.
- Normal data
- Typical, valid data that the program should accept.
- Boundary data
- Data at the edge of the valid range, or just outside it.
- Erroneous data
- Data that is wrong and should be rejected.
- Test plan
- A list of tests with the data, the expected result and the actual result.
Quick questions on Robust programs and testing
- What does a range check do?
- Checks that a value is between a lower and an upper limit: For example, a mark from 0 to 100.
- Which check makes sure that a field is not left blank?
- Presence check: It checks that something has been entered.
- What is verification?
- Checking data has been entered correctly, such as typing it twice: It catches typing mistakes.
- What is authentication?
- Checking that a user is who they say they are: Usernames and passwords are a common method.
- A program is missing a closing bracket and won't run. What type of error is this?
- Syntax error: It breaks the rules of the language.
- A program runs, but it calculates the wrong total. What type of error is this?
- Logic error: The code is valid, but the logic is wrong.
- Valid ages are 11 to 18. Which of these is boundary data?
- 18: 18 is at the edge of the valid range.
- Valid ages are 11 to 18. Which of these is erroneous data?
- Twelve: It is text, not a number, so it should be rejected.
- What is normal test data?
- Typical valid data that the program should accept: For example, 14 for ages 11 to 18.
- Why do programmers write comments?
- To explain the code so it is easier to maintain: Comments are ignored when the program runs.
- What is iterative testing?
- Testing during development, as each part is written: Final testing happens at the end.
- What does a test plan include?
- The test data, the expected result and the actual result: It shows the program has been tested properly.
- Which line has a syntax error?
- Line 2: Line 2 is missing the colon at the end: for i in range(3):. A syntax error breaks the rules of the language, so the program will not run.
- This should print the average of two marks, but prints 100.0. What is the error?
- Brackets are missing: it divides before it adds: Division happens before addition, so it works out 60 + 40. It needs (mark1 + mark2) / 2. The program runs, but gives the wrong answer: a logic error.
- What kind of validation check is this?
- A range check: It checks the value is between two limits (11 and 16) and asks again until it is.
- Which of these is boundary test data for this check?
- 100: Boundary data is at the very edge of what is allowed: 0 or 100. 50 is normal data, 150 is invalid (erroneous) and "ten" is the wrong type.
- The comment says what this should do. What is wrong?
- It stops at 4: it needs range(1, 6): range stops before its second number, so range(1, 5) gives 1, 2, 3 and 4.
Robust programs and testing: exam-style questions and mark schemes
Which validation check would reject a password shorter than 8 characters? [1 mark]
Answer: Length check
A program accepts a test score from 0 to 50. Give one example each of normal, boundary and erroneous test data. [3 marks]
- Normal: any whole number from 1 to 49, e.g. 25 (1)
- Boundary: 0 or 50 (or −1 or 51, just outside) (1)
- Erroneous: e.g. 'abc' or 70 (1)
Explain the difference between a syntax error and a logic error. [2 marks]
- A syntax error breaks the rules of the language, so the program won't run (1)
- A logic error lets the program run, but it produces the wrong result (1)
Describe three ways a programmer can make their code easier to maintain. [3 marks]
- Comments that explain what the code does (1)
- Meaningful names for variables and subroutines (1)
- Indentation, subroutines or constants (1)
Write code that asks the user to enter a password, and keeps asking until the password is at least 8 characters long. [3 marks]
- Asks the user to input a password (1)
- Uses a loop that repeats while the length is less than 8, using len() or .length (1)
- Asks for the password again inside the loop (1)
A cinema's program asks people to type their age to buy a ticket. Describe how the program could be made robust, and the test data you would use to check it. [6 marks]
- validate the input: a type check that it is a whole number
- a range check, such as between 0 and 120
- a presence check, so it can't be left empty
- if it is invalid, show a clear message and ask again (in a loop) instead of crashing
- normal test data, such as 15
- boundary test data, such as 0 and 120, and 121 just outside
- erroneous test data, such as "twelve" or -5
- make it maintainable: comments, meaningful names and indentation
How to answer them: exam technique and command words.
Robust programs and testing: questions and answers
- What is normal test data?
- Typical valid data that the program should accept. For example, 14 for ages 11 to 18.
- Why do programmers write comments?
- To explain the code so it is easier to maintain. Comments are ignored when the program runs.
- What is iterative testing?
- Testing during development, as each part is written. Final testing happens at the end.