Edexcel International Advanced Level Computer Science, point 19.2.1
Consistency (9.2.1) means applying the same style everywhere in a program, so that it reads as if one person wrote it. In larger Unit 4 programs (several files, many subprograms) consistency matters even more. The specification refers to the Good Programming Practice Guide (GPPG). Python's PEP 8 is the usual model.
The style checklist
| Area | Rule |
|---|---|
| a. White space | 4 spaces per indent level; spaces around operators and after commas; no stray spaces inside brackets; blank lines between subprograms (two at top level) |
| b. Line breaks and layout | One statement per line; imports at the top, then constants, then subprograms, then the main program; break long lines inside brackets; one blank line between logical groups |
| c. Naming conventions | snake_case for variables, functions and modules; UPPER_CASE for constants; CapWords for classes; booleans read like a question (is_valid); the same word for the same idea everywhere |
| d. Fixed line length | No line longer than 79 characters (or the team's chosen limit) |
A consistent file layout
"""Stock calculations.""" # a module docstring
import math # 1. imports
from decimal import Decimal
LOW_LEVEL = 5 # 2. constants
VAT_RATE = Decimal("0.20")
def calculate_vat(price): # 3. subprograms
return price * VAT_RATE
def is_low(quantity, level=LOW_LEVEL):
return quantity < level
def main(): # 4. the main program
print(calculate_vat(Decimal("10.00")))
if __name__ == "__main__":
main()
Consistency across the whole program
Choose one convention and use it in every file. Common slips:
| Inconsistency | Fix |
|---|---|
getName() in one file and get_name() in another |
get_name everywhere |
customer, cust, client for the same thing |
Choose customer |
| Tabs in one file, spaces in another | Spaces everywhere |
| Some functions have docstrings, some don't | All public functions do |
Quotes " mixed with ' for no reason |
Pick one style |
One module returns None for "not found", another raises an error |
Use one approach |
| Some lines 40 characters, some 140 | Wrap at 79 |
Breaking long lines
# a long call, broken inside brackets
result = calculate_total(unit_price, quantity,
discount_rate, delivery_cost)
# a long condition, broken inside brackets
if (customer.is_member and order.total > MIN_ORDER
and not order.has_discount):
apply_discount(order)
# a long string, joined in brackets
message = ("Dear customer, your order has been dispatched "
"and will arrive within three working days.")
Tools that enforce consistency
| Tool | Job |
|---|---|
| A formatter (black, autopep8) | Rewrites code to a standard layout automatically |
| A linter (flake8, pylint) | Reports style breaches (long lines, bad names) and likely errors |
| The IDE's format command | Reformats the open file |
.editorconfig |
Shared settings (indent size, line endings) for a team |
$ flake8 stock.py
stock.py:14:80: E501 line too long (92 > 79 characters)
stock.py:20:1: E302 expected 2 blank lines, found 1
Worked example
Task: list the consistency problems in this fragment, then rewrite it.
import os,sys
maxItems=10
def Add_Item(Stock,item_Name,QTY):
if len(Stock)>=maxItems: return False
else:
Stock[item_Name]=QTY;return True
Problems:
| Problem | Rule broken |
|---|---|
os,sys with no space; unused imports |
White space; tidy imports |
maxItems |
A constant should be UPPER_CASE |
Add_Item, Stock, item_Name, QTY |
Mixed naming styles (should be snake_case) |
2-space indent, and return on the if line |
4-space indent; one statement per line |
No spaces around >= and = |
White space |
; joining two statements |
One statement per line |
| No blank lines around the function | Layout |
Rewritten:
MAX_ITEMS = 10
def add_item(stock, item_name, quantity):
if len(stock) >= MAX_ITEMS:
return False
stock[item_name] = quantity
return True
Exam tip: give specific rules (4-space indentation, snake_case, UPPER_CASE constants, lines under 79 characters). Be consistent in your own code, as examiners can see it, and it helps you spot your own errors.
Check yourself
What naming convention is used for constants in Python?
UPPER_CASE with underscores.
Why is a maximum line length used?
Lines fit on screen and in side-by-side views without scrolling, and short lines are easier to read and compare.
What does a linter do?
It checks code for style problems and likely errors and reports them.
Why does consistency matter more in larger programs?
Many files and people are involved, so a shared style keeps the whole program predictable and easy to read.