Following meaning
Lesson 13 Names as signposts
when to trust a name, and names that lie
What you'll learn
- Treat a name as a claim and check it against the body
- Spot names that lie, from is_ that isn't a boolean to get_ that writes
- Notice names that hide units and offsets
Names are the cheapest signposts in code, and the cheapest to get wrong. AI picks tidy names fast. Your job is to check the few that you will rely on.
The idea
A name is a claim. isActive claims a yes or no. getCart claims it only reads. validateOrder claims it only checks. Most claims are true, and that's why you can read fast. Trust a name until it touches money, data or a decision, then check the body against the claim.
Watch for names that lie. An is or has that returns a string or a number. A get that also writes. A validate that changes its input. A plural that isn't a list. A number with no unit.
1function isActive(user) {2 return user.status && user.status.toLowerCase();3}45function getCart(userId) {6 let cart = carts.get(userId);7 if (!cart) {8 cart = { items: [] };9 carts.set(userId, cart);10 }11 return cart;12}1314function validateOrder(order) {15 order.items = order.items.filter((i) => i.qty > 0);16 return order.items.length > 0;17}
- Line 2
isActivepromises a boolean but returns the status text, or undefined. Even "banned" is truthy, soif (isActive(user))lets banned users in. - Line 9
getCartclaims to read, but it creates and stores a cart when there is none.getOrCreateCartwould be honest. - Line 15
validateOrderclaims to check, but it removes items from the order. Callers may not expect their input to change.
Take away
- A name is a claim: check the body of the few that matter.
isandhasshould return true or false,getshould only read,validateshould only check.- Ask what a name leaves out: units, offsets, whether it changes its input.
Quiz: test yourself
7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.