Edexcel International Advanced Level Computer Science, point 9.1.1
Best practice means the methods that experienced programmers use to write code that is correct, readable and maintainable. This point is about producing correct outcomes. The specification points to the Good Programming Practice Guide (GPPG) for detail, which isn't reproduced here, so use it alongside these notes.
a. Problem decomposition into component parts
Break the problem into smaller parts (10.1.2), and solve each one separately, then combine them.
- Each part has one clear job, which makes it easier to write and test.
- Write the main program as a short list of steps that call the parts.
def read_orders():
... # get the data
def calculate_total(orders):
... # work out the result
def print_receipt(total):
... # display it
orders = read_orders()
print_receipt(calculate_total(orders))
b. Clear and appropriate logic
The steps and decisions must be correct and easy to follow.
- Plan before coding (flowchart, pseudocode, comments).
- Make conditions precise, with the right operator and boundaries (
>=or>). - Test conditions in the right order (most specific first).
- Avoid unnecessary complexity: choose the simplest logic that works.
- Check the logic with a trace table (5.1.2).
Unclear logic:
if not (age < 18):
ticket = "adult"
else:
ticket = "child"
Clearer:
if age >= 18:
ticket = "adult"
else:
ticket = "child"
c. Variables, constants, data types and data structures appropriate to the problem
| Choice | Good practice | Example |
|---|---|---|
| Variables | Meaningful names, one purpose each | total_price, not x |
| Constants | Name fixed values | MAX_SEATS = 30 |
| Data types | Match how the data is used | An ID or phone number as a string, a count as an integer |
| Data structures | Choose the structure that fits | A dictionary to look up by name, a set for unique items, a 2D list for a grid |
Poor choice: two parallel lists (names and marks) that must stay in step. Better: a dictionary {name: mark} or a list of records.
d. Programming constructs appropriate to the problem
Use the right construct for the job (7.1.1):
| Need | Construct |
|---|---|
| A known number of repeats | for |
| Repeat until something happens | while |
| Choose between actions | if / elif / else |
| Handle errors | try / except |
| Repeat a block of steps in different places | A subprogram |
Don't use a while loop with a manual counter when a for loop does the same job:
# clumsy
i = 0
while i < len(names):
print(names[i])
i = i + 1
# better
for name in names:
print(name)
Checking that the outcomes are correct
- Test with normal, boundary and erroneous data (9.3.1).
- Compare results with values worked out by hand.
- Trace tricky code.
- Re-read the requirements: does the program do everything asked?
Worked example
Task: improve this code, which should give the average of the temperature readings below freezing.
r = [3, -2, 5, -6, 1]
t = 0
c = 0
for i in range(0, 5):
if r[i] < 0:
t = t + r[i]
c = c + 1
print(t / c)
Problems: unclear names, a fixed 5 that breaks if the list changes, an unnamed boundary 0, and no handling of the case where no readings are below freezing (c is 0, so division by zero).
Improved:
FREEZING = 0
def average_below(readings, limit):
cold = [r for r in readings if r < limit]
if len(cold) == 0:
return None
return sum(cold) / len(cold)
readings = [3, -2, 5, -6, 1]
print(average_below(readings, FREEZING)) # (-2 + -6) / 2 = -4.0
- It is decomposed into a subprogram that does one job.
- The names and constant explain the meaning.
- The logic handles the empty case, so it is correct for all data.
- The data structure is a list, and a comprehension selects the items.
Exam tip: when asked how code could be improved to produce correct outcomes, give specific changes (a named constant, a check for an empty list, a subprogram) and say what error each prevents. Show you've thought about unusual data, not just typical cases.
Check yourself
Why decompose a problem into parts?
Each part is simpler to write and test, and the program is easier to understand and correct.
Why use a named constant instead of the number itself?
It explains the meaning, can be changed in one place, and reduces typing errors.
Give an example of choosing a data structure that fits the problem
A dictionary to look up a price by product code, or a set to hold unique items.
What could cause a division by zero when finding an average?
An empty list or no matching items, so the count is 0; the code should check for this first.