What’s in a Name?
Good names increase design energy and momentum. You can build on a good name. When the name of a software object is spoken, designers infer something about an object’s role and responsibilities. That’s why grizzled object designers say, “Choose names carefully.” A well-formed name creates a link to past experience and common practice. Meaning comes along with any name, whether we like it or not. Our brains are wired to find connections to things we already know. So the key to giving an object a good name is to make its name fit with what you already know while giving a spin on what it should be doing. Most names fit into a system of names. Different naming schemes coexist, even within a single application. There isn’t one universal naming system.
“... the relation of thought to word is not a thing but a process ... Thought is not merely expressed in words; it comes into existence through them. Every thought tends to connect something with something else, to establish a relationship between things. Every thought moves, grows and develops, fulfills a function, solves a problem.”
A Calendar represents a system of dates and time at a particular location. GregorianCalendar extends the Calendar class. Following convention, we could invent JulianCalendar or ChineseCalendar classes. Others familiar with this scheme could make educated guesses about how their implementations would differ from GregorianCalendar.
Include only the most revealing and salient facts in a name. The downside of any descriptive scheme is that names can become lengthy. Don’t name every distinguishing characteristic of an object; hide details that might change or should not be known by other objects.
Should people really have to care that they are using a MillisecondTimerAccurateWithinPlusOrMinusTwoMilliseconds, or will Timer suffice? Detailed design decisions should not be revealed unless they are unlikely to change and they have a known impact on the object’s users. Exposing implementation details makes them hard to change.
Consider the Singleton pattern described in the Design Patterns book. This pattern ensures that a class has only one instance with a global point of access. We could name every concept that applies this pattern a MumbleMumbleSingleton. Following our guideline, we recommend against this. Singleton is a distinction that is more important to a class implementer than to a client who uses a singleton. Give names that will be meaningful to those who will be using the candidate, not those who will be implementing it. If someone using your candidate must know the details of its implementation, you have likely missed an opportunity to do a better job of abstraction. One possible exception to this rule is to append Singleton to a class name when it is crucial for its users to know this.
Give service providers “worker” names. Another English language naming convention is to end job titles with “er.” Service provider objects are “workers,” “doers,” “movers,” and “shakers.” If you can find a “worker name,” it can be a powerful clue to the object’s role.
Many Java service providers follow this “worker” naming scheme. Some examples are StringTokenizer, SystemClassLoader, and Applet-Viewer.
If a worker-type name doesn’t sound right, another convention is to append Service to a name. In the CORBA framework, this is a common convention—for example, TransactionService, NamingService, and so on.
Look for additional objects to complement an object whose name implies broad responsibilities. Sometimes a candidate represents a broad concern; sometimes its focus is more narrow. If you come across a name that implies a large set of responsibilities, check whether you’ve misnamed a candidate. It could be that your candidate should have a narrower focus. Or it might mean that you have uncovered a broad concept that needs to be expanded. Looking for objects that round out or complement a broad name can lead to a family of related concepts—and a family of related candidates. Many times we need both specific and general concepts in our design. The more generic named thing will define responsibilities that each specific candidate has in common.
An object named AccountingService likely performs some accounting function. The name AccountingService isn’t specific. We cannot infer information about the kinds of accounting services it performs by looking only at its name. Either AccountingService is responsible for performing every type of accounting function in our application, or it represents an abstraction that other concrete accounting service objects will expand upon. If this is so, we’d expect additional candidates, each with a more specific name such as BalanceInquiryService, PaymentService, or FundsTransferService. These more specifically named candidates would support specific accounting activities.
Highlight a general concept with more specific candidates. If you can think of at least three different special cases, keep both the general concept and specific ones. If later on, you find that these more specific candidates don’t share any responsibilities in common, the more abstract concept can always be discarded. However, if you have simply assigned a candidate a name that is too generic, by all means rename it.
If your candidate could represent historical records of many other things, better to leave it with a more generic name, History, instead. If you intend to model transaction history, rename your candidate TransactionHistory. You decide how specific you want to be.
Forming an abstraction by looking at two specific cases might work, but comparing and contrasting three or four cases is even better. The more closely related concepts you can compare and contrast in order to identify what they have in common, the better.
Therein lies the art of naming: choosing names that convey enough meaning while not being overly restrictive. Leave open possibilities for giving a candidate as much responsibility as it can handle, and for using it in different situations with minor tweaks. It certainly is a more powerful design when a candidate can fit into several different situations. The alternative—having a different kind of object for each different case—is workable, but not nearly so elegant.
Choose a name that does not limit behavior. Don’t limit a candidate’s potential by choosing a name that implies too narrow a range of actions. Given the choice, pick a name that lets an object take on more responsibility.
Our thoughts shape our words, and our words influence our thoughts. Names subtly shape our ideas about our candidate’s expected behaviors.
Consider two alternatives for a candidate: Account or AccountRecord. Each could name an object that maintains customer information. From common knowledge we know one meaning of record is “information or facts set down in writing.” An AccountRecord isn’t likely to have more than information holding responsibilities if we fit its role to conventional usage of this name. The name Account, however, leaves open the possibility for more responsibilities. An Account object could make informed decisions on the information it represents. It sounds livelier and more active than AccountRecord.
Choose a name that lasts for a candidate’s lifetime. Just as it seems funny to hear a 90-year old called “Junior,” it’s a mistake to name a candidate for its earliest responsibilities, ignoring what else it may do later on. And don’t be content to stay with the first name you give a candidate if its work changes.
An object that defines responsibilities for initializing an application and then monitoring for external events signaling shutdown or re-initialization, is better named ApplicationCoordinator than ApplicationInitializer. ApplicationInitializer doesn’t imply having ongoing responsibilities after the application is up and running. ApplicationCoordinator is a better name because its more general meaning encompasses more responsibilities.
Choose a name that fits your current design context. When you choose names, select ones that fit your current design surroundings. Otherwise, your candidates’ names may sound strange. What sounds reasonable in an accounting application may seem jarring in an engineering application.
A seasoned Smalltalker tried hard to set aside his biases when he started working with Java. Although he expected Java classes to have totally different responsibilities, he was surprised to find the Java Dictionary class to be abstract. In Smalltalk, Dictionary objects are created and used frequently.
Shed your past biases when they don’t fit your current situation.
Do not overload names. Unlike spoken language, where words often have multiple meanings, object names should have only one meaning. It isn’t good form to have two different types of Account objects with radically different roles that coexist in the same application. Some object-oriented programming languages let you assign the same name to different classes but then force you to uniquely qualify a name when you reference a particular class in code. In Java, for example, classes from different packages can have the same name. In order to uniquely designate a specific one, its name must be qualified by the name of the package where it is defined.
A Java designer can define classes with the same name, each residing in a different package. You should do so only if one package is designed as a replacement for another.
Names of things that can simultaneously coexist within a single application should be given different names. Don’t overload a name. Programmers have only one context—the running application—in which to interpret names. They already have enough to think about without adding yet another source of confusion. Compilers are good at automatically applying the correct qualification to a name. Humans aren’t!
Eliminate name conflicts by adding an adjective. Sometimes the best names are already chosen. Still, you need to name your candidate. By adding a descriptive phrase to a name, you can come up with a unique name.
The candidate TransactionProperties might be a reasonable name for a candidate whose preferred name conflicts with the preexisting Java class named Properties.
A word of caution: If your candidate has a radically different meaning, don’t co-opt a familiar name. Follow convention. Designers familiar with existing names will expect your candidate to fit in and work similarly.
Eliminate conflicts by choosing a name with a similar meaning. Sometimes, your best bet is to look for a synonym. Each synonym has a slightly different shade of meaning, so finding a satisfactory name may be hard.
The synonyms for Property, a class defined in the Java libraries, include these words: characteristic, attribute, quality, feature, and trait. Although “attribute” or “feature” might work, “characteristic” seems stuffy, and “quality” seems strained.
Choose names that are readily understood. A name shouldn’t be too terse. Don’t encode meaning or cut corners to save keystrokes. If you want others to get a sense of an object’s role without having to dig into how it works, give it a descriptive name. A name can be descriptive without being overly long.
“Acct” is too cryptic. “Account” is better.
If your problem domain has well-known and understood abbreviations—such as USD in banking, or Mhz or Gbyte in technology—it is reasonable to include these in a candidate’s name.