Chapter 2 Flashcards — Meaningful Names
What is the “intention-revealing names” rule?
?
A name should answer three questions: why it exists, what it does, and how it is used. If a name requires a comment to explain it, the name has already failed. elapsedTimeInDays is self-explanatory; d requires a comment and a mental lookup every time it is read.
What is wrong with naming a HashSet accountList?
?
It is disinformation — List implies ordered access, indexed retrieval, and List-specific behavior. Readers will assume accountList.get(0) works. The name should reflect the actual type: accountGroup, accountSet, or accountsByEmail for a Map.
What are “noise words” and why are they harmful in names?
?
Noise words are suffixes like Info, Data, Object, The, Manager that add characters without adding meaning. ProductData and ProductInfo are indistinguishable — neither communicates how they differ from Product. Use names that describe the specific concept: ProductSearchResult, ProductCatalogEntry.
What is the “number series” naming anti-pattern?
?
Using a1, a2, a3 as argument names communicates nothing about the relationship between the arguments. copyChars(char[] a1, char[] a2) hides that a1 is the source and a2 is the destination. copyChars(char[] source, char[] destination) makes it self-documenting.
Why must names be pronounceable?
?
Programming is a social activity — code is discussed in meetings, reviews, and on calls. A name like genymdhms cannot be pronounced without sounding absurd (“gee-en-why-em-dee-aitch-em-ess”). generationTimestamp can be spoken naturally in a sentence. Unpronounceable names slow down communication.
Why are single-letter variable names and bare numbers problematic for searchability?
?
e will match every occurrence of the letter e in a codebase. Searching for MAX_CLASSES_PER_STUDENT finds exactly the right place. Similarly, 7 cannot be grepped meaningfully, but a named constant can be. Searchability matters more than brevity for names used outside tiny local scopes.
When is a single-letter variable name acceptable?
?
Single-letter names are acceptable only as loop counters in tiny, bounded scopes (for (int i = 0; i < 5; i++)) and as conventional symbols in mathematical code (e.g., x and y in geometry). Any name that appears in a scope larger than a few lines, or represents a domain concept, needs a descriptive name.
What is Hungarian Notation and why should it be avoided?
?
Hungarian Notation encodes type information in the variable name: strFirstName, intAge, bIsActive. It was invented for weakly-typed C compilers. In modern type-safe languages with IDEs that show types on hover, it is pure noise — and it becomes actively misleading when the type changes but the name prefix does not (PhoneNumber phoneString).
What is the “member prefix” anti-pattern?
?
Prefixing instance variables with m_ (e.g., m_firstName) was used to distinguish member variables from locals. Modern IDEs highlight scope visually, making m_ prefixes redundant noise that clutters every name. Remove them and let the class itself provide the context.
What is the recommended way to name interfaces and their implementations?
?
Give the interface the clean, unprefixed name (e.g., ShapeFactory). If you must distinguish the implementation, add a suffix to the implementation class (ShapeFactoryImp, BitmapShapeFactory). The I prefix on interfaces (IShapeFactory) encodes an implementation detail into the name and pollutes every call site.
What is the “mental mapping” problem with names?
?
When a variable name requires the reader to mentally translate it to its real meaning (e.g., r → “the HTTP response body”), the reader must hold both the variable name and its meaning in working memory simultaneously. This is avoidable cognitive load. responseBody requires no translation.
What should class names look like, and what words should be avoided?
?
Class names should be noun phrases that describe what the object is: Customer, Account, InvoiceGenerator, AddressParser. Avoid Manager, Processor, Data, Info — they are too vague to distinguish any class from any other. Never use a verb as a class name.
What should method names look like?
?
Method names should be verb phrases that describe the action: postPayment, deletePage, generateInvoice. Use consistent prefixes: get/set for accessors/mutators, is/has for boolean predicates. Static factory methods should describe what they return: Complex.fromRealNumber(5.0) is more expressive than new Complex(5.0).
What is the “don’t be cute” rule?
?
Humorous or culturally specific names (HolyHandGrenade, whack(), eatMyShorts()) only work if every current and future reader shares the reference. Clarity beats cleverness. Use deleteItems(), kill(), abort(). Code is read under stress — the reader does not want to decode jokes.
What is the “one word per concept” rule?
?
Pick one verb for each concept and use it consistently across the entire codebase. If getUser(), fetchOrder(), and retrieveProduct() all do the same thing (look up an entity by ID), they should all be get. Inconsistency forces readers to wonder if the three words imply subtle behavioral differences — they should not have to wonder.
What is the difference between “pick one word per concept” and “don’t pun”?
?
One word per concept says: use the same word for the same concept across all classes. Don’t pun says: do not reuse the same word for two different concepts. add meaning “add to a collection” in one class and “concatenate strings” in another is a pun — the same word with two meanings. The solution is append or insert for the second concept.
What are “solution domain names” and when should you use them?
?
Solution domain names are CS terminology that developers already know: Visitor, Queue, Factory, Singleton, EventBus, Iterator. Use them when the pattern or data structure accurately describes what you are implementing. A developer reading AccountVisitor immediately understands the interaction pattern without reading the implementation.
What are “problem domain names” and when should you use them?
?
Problem domain names come from the business or subject area: AmortizationSchedule, FiduciaryAccount, NsfFee (non-sufficient funds). Use them when no CS term accurately describes the concept. If the developer does not know the term, they can ask a domain expert — which is better than having both parties confused by an invented internal name.
What is the “add meaningful context” rule?
?
Some names are ambiguous in isolation. A class provides context for its members: state inside Address is unambiguously a geographic state. state in a method with no enclosing class could mean anything. When you cannot use a class, a short prefix (addrState) is a last resort — but prefer creating the class.
What is the “no gratuitous context” rule?
?
Do not prefix every class in an application with a project-wide abbreviation (e.g., GSDAccountAddress for every class in the “Gas Station Deluxe” app). Shorter names are better than longer ones as long as they remain clear. AccountAddress is always cleaner than GSDAccountAddress. Add context only when the shorter name would genuinely be ambiguous to a reader.
When would you choose MailingAddress over Address?
?
When the codebase also has BillingAddress and ShippingAddress. Context is only needed when ambiguity exists. If Address is always a mailing address in your domain, Address is the right name. When multiple address types exist, the distinguishing word (Mailing, Billing, Shipping) adds meaning rather than noise.
What does it mean that a name “adds a comment at every read site”?
?
A name that requires a comment to explain it imposes a reading tax at every site where it is used — not just the declaration. int d; // elapsed time in days saves one line at declaration but costs understanding at every read. int elapsedTimeInDays pays once and is free at every read.
What is wrong with naming a class DataManager?
?
DataManager is too vague to be meaningful — nearly every class manages some data. It reveals nothing about what kind of data, what management operations are supported, or what the class’s responsibilities are. A precise name like OrderRepository, UserSessionCache, or TransactionLedger communicates all three immediately.
What naming convention signals a boolean method?
?
Boolean predicates should begin with is or has: isActive(), isExpired(), hasPermission(role), isEmpty(). This prefix signals to the caller that the method returns a boolean, making conditions read like English: if (user.isActive()) is clearer than if (user.active()).
What is the risk of distinguishing names only by a single character?
?
Names like XYZControllerForEfficientHandlingOfStrings and XYZControllerForEfficientStorageOfStrings differ by one word buried in the middle. Readers scan, they do not parse — such names are functionally indistinguishable at a glance and will be confused in code reviews, grep results, and auto-complete lists.
Why is the variable l (lowercase L) particularly dangerous as a name?
?
In most fonts, lowercase l is visually identical to the digit 1. Similarly, uppercase O looks like 0. Code like if (O == l) reads as if (0 == 1) at first glance. These one-character names cause real bugs and are one of the only cases where a name is actively worse than a single arbitrary letter.
What does consistent naming across a codebase enable that inconsistent naming does not?
?
Predictability — a developer learning the codebase can correctly guess names they have never seen. If every entity is retrieved with get, every entity is saved with save, and every boolean is checked with is, the developer can navigate new modules confidently. Inconsistency forces discovery at every new class.
What is the underlying principle that connects all 16 naming rules in Chapter 2?
?
Every rule is a specific instance of the same principle: names exist to communicate to the reader, not to the compiler. The compiler accepts any name; a human reader needs names that reveal intent, avoid false implications, and fit into a consistent vocabulary. Every rule addresses one dimension of that communication.
Total Cards: 28
Review Time: ~22 minutes
Priority: HIGH
Last Updated: 2026-04-14