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
| Word | What it means |
|---|---|
| Naming convention | an agreed style for names, such as lower case with underscores |
| Comment | a note in code that the computer ignores and people read |
| Logic | the rules that decide what happens |
| Boundary | the exact edge between two cases, such as 49 and 50 points |
| Function | a named block of code that takes a value and gives one back |
| SQL | a language for asking questions of data kept in tables |
| Attribute | one fact about an entity, such as a name |
| Entity | a 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 questionsYour 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
| Weak | Better | Why |
|---|---|---|
| x2 | discounted_price_pence | Says what it is and its unit |
| d | full_price_pence | No guessing |
| CustData | CUSTOMER | Entity names are singular, one style |
| Table1 | SALE_LINE | Says 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
# 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.