Defensive Design & Testing Flashcards
All 36 cards in this deck
What is defensive design?
Designing a program so it anticipates misuse and errors and keeps working (is robust) whatever the user enters.
What is input sanitisation?
Removing invalid or special characters from data entered by the user, e.g. removing quotation marks or semicolons.
What is authentication in a program?
Checking that users are allowed to access the system / confirming the identity of the user, e.g. a username and password.
True or false? A program is well designed defensively if it crashes as soon as a user types a letter into a number box.
False. Defensive design means the program handles the unexpected input, e.g. rejects it and asks again, instead of crashing.
What are the steps to add simple username and password authentication?
e.g. logging in to a school booking system- Ask the user to input a username and a password.
- Compare what is entered with the details stored for that user.
- If both match, allow access; if not, display an error and ask again.
What is input validation?
A check that only sensible/allowed data is entered into a program, e.g. restricting a rating to 1 to 10.
What does a range check do?
e.g. a height in cm must be from 40 to 180Checks the value entered lies between a stated lower and upper limit — here, that the height is at least 40 and no more than 180.
What does a presence check do?
Checks that data has actually been entered and the field has not been left blank.
What does a length check do?
Checks that the data entered has an allowed number of characters, e.g. a password of at least 8 characters.
What does a type check do?
Checks that the data entered is of the correct data type, e.g. an integer for an age.
What does a format check do?
Checks that the data entered follows a set pattern, e.g. a postcode or a date as dd/mm/yyyy.
What should a program do when it detects invalid data?
Reject the data, output a helpful error message and ask the user to enter it again, rather than crashing or storing it.
What are the steps to write an algorithm that validates an input and re-prompts until it is valid?
e.g. a height in cm must be from 40 to 180- Input the value into a variable:
height = input("Enter height") - Loop while the value is outside the range:
while height < 40 OR height > 180 - Inside the loop, output an error message and input the value again; the loop ends when the value is valid.
- Input the value into a variable:
True or false?
if height >= 40 AND <= 180is an acceptable way to write a range check.False. The variable must be repeated in each condition:
if height >= 40 AND height <= 180.What is maintainability in defensive design?
Ensuring a program can be understood and changed by other programmers, e.g. by using modularisation and comments.
How does using sub programs help maintainability?
It splits the program into smaller named sections that can be reused, read, tested and updated separately.
How do naming conventions (sensible identifier names) help maintainability?
Meaningful variable and sub program names show what each one stores or does, so the code is easier to follow, e.g.
totalScorerather thanx.How does indentation help maintainability?
It shows which lines are inside a loop, condition or sub program, making the structure of the code clear.
What is a comment in a program, and why is it useful?
A note in the code that is ignored by the translator; it explains what a section of code does so others can understand it.
What is the purpose of testing a program?
To check the program works as expected and gives the correct output for all inputs, and to find and remove errors before release.
What is iterative testing?
Testing modules or sections of a program repeatedly while it is being developed, fixing errors as they are found.
What is final (terminal) testing?
Testing the complete program at the end of production to check it meets the original requirements and all modules work together.
What columns should a test plan contain?
The test data used, the purpose/type of test, the expected result and the actual result.
What should a programmer do when the actual result of a test does not match the expected result?
Find and correct the error in the code (refine the algorithm), then run the test again to check it now passes.
Give one reason for refining an algorithm other than to correct an error.
To make it more efficient (fewer steps / faster) or more robust so it copes with unexpected inputs.
What is a syntax error?
An error that breaks the grammatical rules of the programming language, so the program cannot be run or translated.
What is a logic error?
An error that lets the program run but produces unexpected or incorrect output.
Give one example of a syntax error in code.
A misspelled keyword or a missing bracket/quotation mark, e.g.
prnt("Hello").Give one example of a logic error in code.
Using the wrong operator, e.g.
total = a - bwhen the total should bea + b, or<where<=is needed.What is normal test data?
Data which should be accepted by the program without causing errors.
What is boundary test data?
Data of the correct type which is on the very edge of being valid, i.e. at either edge of the accepted range.
What is invalid test data?
Data of the correct data type which should be rejected by the program.
What is erroneous test data?
Data of the incorrect data type which should be rejected by the program, e.g. letters entered where a number is required.
A program accepts a height jumped from 40 cm to 180 cm. Which values are the boundary test data, and what output is expected?
40 and 180 — both should output "VALID".
A program accepts a height jumped from 40 cm to 180 cm. Give one erroneous test value and the expected output.
A non-numeric value such as
abc— expected output "NOT VALID".True or false? Writing "a value between 40 and 180" in the test data column of a test plan is acceptable.
False. Test data must be actual data, e.g. 50, not a description of the data.