Read Code Fast

Lesson 13 Names as signposts

5 min read · Python · Quiz

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.

cart.py
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
  1. Line 2 is_active promises a boolean but returns the status text, or None. Even "banned" is truthy, so if is_active(user): lets banned users in.
  2. Line 6 get_cart claims to read, but setdefault creates and stores a cart when there is none. get_or_create_cart would be honest.
  3. Line 10 validate_order claims 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_ and has_ 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.