Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

AESP task 4a: Clear names, comments and exact logic

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

60 minutes

What is x2?

You open a file and see: x2 = d * 0.9. What is x2? What is d? Why 0.9?

The person who wrote it knew. A week later, even they do not. Names and comments are notes to the next reader, and the next reader is often you.

Key words

WordWhat it means
Naming conventionan agreed style for names, such as lower case with underscores
Commenta note in code that the computer ignores and people read
Logicthe rules that decide what happens
Boundarythe exact edge between two cases, such as 49 and 50 points
Functiona named block of code that takes a value and gives one back
SQLa language for asking questions of data kept in tables
Attributeone fact about an entity, such as a name
Entitya thing the system keeps facts about

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can choose clear names for attributes and entities, write comments that explain why, and get the logic right at the edges.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

This is practice

Important

The real task comes from the exam board. This lesson uses an invented organisation and its own made-up data. Your teacher tells you how the real tasks are set and what you may bring.

Worked example: names that say what they hold

WeakBetterWhy
x2discounted_price_penceSays what it is and its unit
dfull_price_penceNo guessing
CustDataCUSTOMEREntity names are singular, one style
Table1SALE_LINESays what the rows are
f()points_for_spend()A function is named for its job

The cells students fill in are left blank in this preview.

Run it: weak and better

Code: python
# Weak
d = 250
x2 = d * 90 // 100
print(x2)

# Better: the names and the comment say it all
full_price_pence = 250
# Loyalty members pay 90 percent of the price. Whole pence only.
discounted_price_pence = full_price_pence * 90 // 100
print(discounted_price_pence)

Comments say why, not what

A comment that repeats the code helps nobody: "add 1 to total". A comment that gives the reason does: "points start at one for a first visit".

Comment every function, every rule that is not obvious, and every choice a reader might question. In SQL, a comment starts with two dashes.

Exact logic at the edges

Most logic faults are at the boundary. Is 50 points Silver or Bronze? A greater than sign says 50 is not enough. A greater than or equal sign says it is.

Write the rule in words first. Then test the values on each side of every edge: 49, 50 and 51.

The lesson goes on

7 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.