Defensive Design & Testing

💻 Computer Science Years 10–11 Foundation

Anticipating misuse with input validation and authentication, and testing with normal, boundary and erroneous data to find syntax and logic errors.

Open this lesson, with practice and a printable worksheet →

Users are creative in ways you never intend. Ask for an age and someone types "banana", leaves it blank, or enters minus five. A well-built program does not fall over — it expects the mess. Defensive design means writing code that anticipates misuse and mistakes, and testing is how you prove it copes. Together they turn a fragile program into a trustworthy one.

Why this matters: Real users make errors, and some are hostile. Software that crashes, accepts nonsense, or lets anyone in is worse than useless — it loses data and trust. Exams reward you for validating input, choosing sensible test data, and telling a syntax error apart from a logic error.
VALIDATION check age = 30 accepted age = -5 ✗ rejected
Validation is a gatekeeper: good input passes, bad input is turned away.

Input validation: the gatekeeper

Validation checks that data is sensible before the program uses it — like a bouncer checking each guest at the door. There are several kinds of check, and good programs stack several together:

CheckWhat it asksExample
PresenceIs anything there at all?Name box not left blank
TypeIs it the right kind of data?Age is a number, not text
RangeIs it within sensible limits?Age between 0 and 120
LengthIs it too short or too long?Password at least 8 characters
FormatDoes it match a pattern?Email contains an "@"

Authentication: proving who you are

factor 1password(you know)+factor 2phone code(you have)sign in2FA = something you know + something you havea stolen password alone is not enough
2FA: a password (know) plus a phone code (have) — two locks, not one.

Authentication confirms a user is who they claim to be. The commonest method is a password, kept strong by demanding length and a mix of characters. But a password can be stolen, so serious systems add two-factor authentication (2FA): something you know (the password) plus something you have (a code texted to your phone). An attacker would need both.

Defensive design in general

Beyond validation, defensive design is a mindset: write clear code with sensible names, add comments so others (and future you) understand it, and above all never trust input — check it every time. Think of a lifeguard who assumes any swimmer might get into trouble, rather than hoping none will.

Choosing test data

accepted: 0 to 1200120normal e.g. 30boundaryboundary-1erroneous200erroneous
For an age 0–120: normal (30), boundary (0, 120), erroneous (−1, 200).

You cannot test every possible value, so you pick three kinds. For an age field that should accept 0 to 120:

Test dataMeaningExample values
NormalTypical, clearly valid30, 45
BoundaryRight on the edge of allowed0 and 120 (accept); −1 and 121 (reject)
ErroneousClearly invalid, wrong type or range−5, 200, "cat"

Syntax errors vs logic errors

SYNTAX errorPRINT("hi"✗won't runrule brokenLOGIC errorarea = w + h?runs, wrong answershould be w × h
A syntax error stops the program; a logic error runs but is wrong.

A syntax error breaks the rules of the language — a missing bracket or misspelled keyword — so the program will not run at all. A logic error is sneakier: it runs fine but gives the wrong answer, because your reasoning was off (using + where you meant ×). To hunt logic errors, testers use iterative testing — test, fix, test again — and a trace table recording each variable at every step to see where it went wrong.

Worked example — testing a "quantity 1–10" box. Valid range is 1 to 10.
Normal: 5 — should be accepted.
Boundary: 1 and 10 accepted; 0 and 11 rejected — these edges catch the most bugs.
Erroneous: −3, 99, and "abc" — all should be rejected with a clear message.
Testing all three kinds proves the validation both lets good data through and blocks bad data.
Common mistake: confusing a syntax error with a logic error. If the program refuses to run, that is a syntax error. If it runs but the output is wrong, that is a logic error — and no error message will appear, so you must test to find it.

When picking test data, always include the boundary values — the exact edges of what is allowed. Off-by-one bugs love the edges, so 0, 120, and just outside are where errors hide.

Get the printable worksheet →
← Data Structures → Translators & Language Levels
← Sasta Basta home All subjects