Lesson 1 · Unit 01
Names
A name is a promise to the reader
What you practise
- A name says what a thing is for
- Units and booleans in names
- Length against scope
- Names that lie
- Renaming as the first refactoring
Try a question from this lesson
Pick one
A constant holds 60 * 60 * 24 and is passed to a cache as the lifetime of an entry. Which name tells the reader the most?
Correct answer
SECONDS_PER_DAY
Why
It says what the number is and in which unit, so nobody hands it to something that expects minutes. The other three only repeat that it is a value, which the reader knew.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A name is a promise
A name tells the reader what a thing holds or does, so they do not have to open it.
Rename until the call site reads as a true sentence. If the function returns the names of active users, say so. The body did not change; what the reader believes did.
def active_names(users):
names = []
for u in users:
if u["active"]:
names.append(u["name"])
return names
count = len(active_names(team))function activeNames(users) {
const names = [];
for (const u of users) {
if (u.active) names.push(u.name);
}
return names;
}
const count = activeNames(team).length;function activeNames(users: User[]) {
const names: string[] = [];
for (const u of users) {
if (u.active) names.push(u.name);
}
return names;
}
const count = activeNames(team).length;9 more exercises in the app
- 3× type the answer
- 1× fill the blanks
- 2× multiple choice
- 1× true or false
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.