By the end of this lesson, you will:
- Understand the difference between Validation, Verification, and Sanitisation.
- Apply maintainability techniques (Comments, Naming, Constants) to Python code.
- Implement input validation checks (Range, Length, Presence, Lookup) in Python.
- Avoid common exam traps.
The "Unmaintainable" Code
Look at the Python code below. It works, but it is terrible. Can you figure out what it does and why it's bad?
Why it's bad:
- Variables
a,b,care meaningless (what do they store?). - No comments explaining the formula.
- The input prompt
"?"is confusing for the user. - It uses a "Hard-coded Value" (3.142) instead of a Constant. This is often called a "Magic Number".
The "Robust" Version:
Maintainability Theory
Always write comments to explain complex logic. This is for other programmers (or future you) to understand the code.
Use variable names that describe the data. Use total_score, never just x.
Break code into functions (def in Python). This makes it modular and easier to test.
Python doesn't rigidly enforce constants, so use ALL CAPS (e.g., VAT_RATE = 0.2) to tell others "Do not change this". Avoid "Hard-coded values".
Defensive Design: Sanitisation
What is Sanitisation?
Many students confuse validation with sanitisation.
Validation checks if data is sensible (e.g., "Is it 8 chars long?").
Sanitisation cleans the data before processing (e.g., "Remove spaces").
Python Task 1: Sanitisation
Copy this code into your Python editor. It "cleans" a username input by removing banned characters like ; (which hackers use for SQL injection) and spaces.
Theory: Input Validation
Input Validation is checking that data meets a set of rules before the program processes it. It checks if the data is sensible, not necessarily if it's true.
Range Check
Checks if data is within a minimum and maximum limit.
Example: Age between 18 and 100.
Length Check
Checks if data has the correct number of characters.
Example: Password must be 8+ chars.
Presence Check
Checks that data has been entered and is not empty.
Example: Required fields on a form.
Lookup Check
Checks against a list of allowed values.
Example: "M", "F", or "Other".
Format Check
Checks if data follows a specific pattern.
Example: Postcode LL9 9LL.
Type Check
Checks data is the correct type (Integer, Boolean etc).
Example: Entering 'Five' instead of 5.
Main Challenge: Club Entry System
Your Task
Write a Python program for a nightclub entry system. It needs to ask for 3 things and validate them:
- Name (Must not be empty)
- Age (Must be 18 to 30)
- Gender (Must be 'M' or 'F')
Use the "Cheat Sheet" on the right to help you write the checks.
print("Invalid age")
age = int(input("Age: "))
print("Too short")
print("Required")
name = input("Name: ")
print("Invalid")
gender = input("M/F: ")
The "Exam Trap" Quiz
Test your knowledge of the tricky concepts found in the mark scheme.
1. The exam question shows a `while` loop that is perfectly indented. You are asked to improve maintainability. What do you suggest?
2. A random number generator picks a number between 1 and 10. Should you write code to validate this number?
3. We check if a password entered matches the one stored in the database. Is this Validation?