Read Code Fast
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. is_active claims a yes or no. get_cart claims it only reads. validate_order 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.
1def is_active(user):2 return user.get("status") and user["status"].lower()345def get_cart(user_id):6 return carts.setdefault(user_id, {"items": []})789def validate_order(order):10 order["items"] = [i for i in order["items"] if i["qty"] > 0]11 return len(order["items"]) > 0
- Line 2
is_activepromises a boolean but returns the status text, or None. Even "banned" is truthy, soif is_active(user):lets banned users in. - Line 6
get_cartclaims to read, butsetdefaultcreates and stores a cart when there is none.get_or_create_cartwould be honest. - Line 10
validate_orderclaims 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.
is_andhas_should return True or False,get_should only read,validate_should 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.