2.3.1

Defensive design

GCSE Computer Science 2.3.1: anticipating misuse, authentication, input validation, and the four maintainability techniques — sub programs, naming conventions, indentation and commenting.

About 14 minutes to read

Anticipating misuse and authentication

Defensive design means writing a program that behaves sensibly when things go wrong, rather than assuming everything will go right.

Anticipating misuse is the mindset behind it: a programmer should consider that a program caters for all likely input values. Users make mistakes, mistype, leave boxes empty, enter letters where numbers were expected, and occasionally try to break the program deliberately. A program that crashes when a user types "twelve" into an age box has not anticipated misuse.

Dealing with invalid data means the program should reject it and explain the problem, rather than crashing or storing nonsense. The usual pattern is a loop that keeps asking until acceptable data is given.

Authentication confirms the identity of a user before granting access. The simplest form is a username and password checked against stored records; stronger forms add a code sent to a phone, or a fingerprint. It answers a different question from validation: authentication asks who you are, validation asks whether what you typed makes sense.

Defensive design
writing a program that behaves sensibly when things go wrong, anticipating misuse and catering for all likely input values
Input validation
checking that data entered meets set rules before the program uses it
Authentication
confirming that a user is who they claim to be, usually with a username and password

Input validation

Validation checks that entered data is reasonable before the program uses it. The named checks are worth learning by name, because questions often ask which one applies.

Figure: Validation checks
Validation checks whether data is sensible, not whether it is true — a range check will happily accept a wrong but plausible age.

Worked example

A form asks for a password of at least eight characters. Write code that validates it, and say which check is being used.

  1. The requirement is about the number of characters, so this is a length check.
  2. The user must be asked at least once, and repeatedly if the entry is too short — a do ... until loop.
  3. Use .length to count the characters entered.
  4. The loop ends when the length reaches 8, so the until condition is length >= 8.
  5. Give a message explaining the problem, since rejecting data silently is poor design.

Answer: This is a length check. do password = input("Enter a password of at least 8 characters") if password.length < 8 then print("Too short — please try again") endif until password.length >= 8

Common mistake

Validation cannot check that data is correct, only that it is possible. A date of birth of 01/01/1990 passes every check even if the user was born in 1991 — that is verification, which is a different thing.

Maintainability

Maintainable code is code that someone else — or you, months later — can read, understand and change without breaking it. The specification names four techniques.

Figure: The four maintainability techniques

The specification asks specifically why commenting is useful and that you apply it appropriately — and 'appropriately' is doing real work in that sentence. A comment reading count = count + 1 // add one to count is worthless: it repeats what the line already says. A comment reading // discount only applies to orders over £50, as agreed with the client explains something the code cannot.

Comment the reasoning, the parts that are not obvious, and the purpose of each sub program. Do not comment every line.

Maintainability
the quality of code that someone else, or the same programmer months later, can read, understand and change without breaking it

Before you practise

  • Defensive design assumes users will make mistakes and misuse the program.
  • Invalid data should be rejected with an explanation, not allowed to crash the program.
  • Authentication confirms who the user is; validation checks whether the data is sensible.
  • The checks: range, type, length, presence, format and look-up.
  • Validation cannot tell whether data is true, only whether it is plausible.
  • Maintainability comes from sub programs, naming conventions, indentation and commenting.
  • Meaningful names make code self-explanatory.
  • Comments should explain why, not restate what the line does.

Now practise it

Reading is the easy half. Answer questions on 2.3.1 Defensive design and find out which parts you actually know.