Defensive design
Validation checks
Validation checks that data is sensible before a program uses it. A username can be required (presence), limited to 3–12 characters (length) and restricted to letters, digits and underscores (format). A person's age can be checked to be from 11 to 16 (range). Python's input() gives text, so check that it contains digits (type) before converting it. ERL's age example assumes age is entered as an integer.
INPUT username
valid = username != ""
IF username.length < 3 OR username.length > 12 THEN
valid = false
ENDIF
formatValid = true
IF username.length > 0 THEN
FOR i = 0 TO username.length - 1
code = ASC(username.substring(i, 1))
IF NOT ((code >= 48 AND code <= 57) OR (code >= 65 AND code <= 90) OR (code >= 97 AND code <= 122) OR code == 95) THEN
formatValid = false
ENDIF
NEXT i
ENDIF
IF NOT formatValid THEN
valid = false
ENDIF
INPUT age
IF age < 11 OR age > 16 THEN
valid = false
ENDIF
The ERL age input is an integer; Python checks the text before casting.
Key terms
Authentication
Authentication checks that a user is who they claim to be, usually when they sign in. This simple example compares a username and password with demonstration values:
INPUT username
INPUT password
IF username == "learner" AND password == "demo-only" THEN
OUTPUT "Signed in"
ELSE
OUTPUT "Try again"
ENDIF
This is only a classroom example, not a real sign-in system. Real systems never store plain-text passwords; they store salted password hashes and use trusted authentication methods. Validation checks data; authentication checks identity.
Key terms
Maintainable programs
Maintainability means that a program is easy to understand, test and change. Use meaningful names such as age instead of x, consistent indentation, and comments to explain non-obvious decisions. Split repeated tasks into subprograms with clear purposes, and validate input in one place. These habits help someone find and fix an error without accidentally changing a different part of the program.
Remember
Comments explain why a decision was made; clear names and indentation show how the code is organised.