This Grumpy Programmer mailing-list essay got me thinking:

There is one point of contention I have with Laravel -- some of the naming conventions that have causes numerous Twitter flame wars and blog posts. I speak, of course, about facades. ... [T]he facade thing is a warning to remain vigilant and do your research into what everyone else calls something. Common vocabulary is critical when it comes time to solve difficult problems.

I completely agree with the emphasis on using a common vocabulary. One issue here is that naming things properly is very, very hard. It is one of the only two hard problems in programming.

Are there any rules (even rules-of-thumb) that can we use to make it easier to pick good names for the classes and concepts in our projects?

What's In A Name?

Above all, a good name makes it obvious what we are describing. It provides clarity by itself without the need for further explanation.

However, because a word can be ambiguous, we have to pick the proper context for the use of a name. Using the right context when picking a name is paramount.

Unfortunately, in programming, there may be overlapping contexts. Giving a name to a domain class or concept can be especially difficult because of this overlap. Do we name an element for what the domain experts call it in their normal day-to-day work, or for its layer in the overall system, or for the pattern of its design?

Not A Novel Context

The first thing we have to remember, then, is that we are not writing a novel in English. Instead, we are writing software instructions. We are writing these instructions in something other than a normal human language. We are writing these instructions for a computer, and simultaneously writing them for human readers of that programming language.

With that in mind, I suggest the following order of priority for picking the proper context for a name:

  1. First is the vocabulary, common idioms, and core resources of the programming language itself. If the language itself, its core classes and methods, or the common expressions of its professional programmers associate particular vocabulary words with a concept, activity, or behavior, we should choose from among those words first when attempting to pick a name.

  2. Second is the vocabulary and common idioms related to previously identified patterns of software design. If there is a software design pattern that matches the concept we are trying to express, its name should be our next-best choice.

  3. Third is the vocabulary, idioms, jargon, and slang related to the domain. If there is no appropriate word from the programming language, and no relevant software design pattern, then falling back to the domain for the naming context is our next priority.

  4. The absolute last context should be words from normal everyday interactions. We use these words only after exhausting the other three contextual options. This is the least-appropriate context for picking a name.

This priority order is probably the opposite of what many would prefer. As human beings, we are more fluent and familiar with the vocabulary of our everyday writing and speech. It took many years of hard work to acquire that vocabulary in the first place, and we are unwilling to do additional hard work to acquire yet another vocabulary that applies more appropriately the context of programming. We would prefer to reuse the words we already know, instead of learning how to apply new words.

Even so, to be good at communicating the concepts of software programming to other programmers, it is incumbent upon us as professionals to expand our vocabulary. We must learn these new terms and their proper use. This is especially difficult with patterns of software development.

Patterns Are Defined By Behavior

When it comes to patterns, we have to remember that the patterns themselves pre-exist their names. The names were applied only after seeing the same patterns over and over again in many different programs. This means that it is the behavior of a construct that determines what its appropriate pattern-related name should be. If we write code like this ...

class Foo
    public function newInstance($bar)
        return new $bar;

... and we never mention the word "factory", the code is still representative of the Factory pattern. A rose by any other name may smell as sweet, but if we call it a daisy then nobody will be able to guess that it has thorns. Similarly, a "factory" by any other name still creates and returns objects. Calling the above construct anything other than "factory" (or a factory-related name) removes clarity instead of providing it.

Picking The Closest Pattern

As a related issue, it's not enough to look through pattern definitions and find one that is "close enough." We must find the best one we can that matches the most parts of the concept we are trying to express in a name. This can be a lot of effort.

By analogy, we should not call a "motorcycle" a "bicycle" and expect to be understood fully. Yes, a "motorcycle" matches up with a "bicycle" in important ways: they both have two wheels, we steer and brake with our hands instead of our feet, we sit on a seat above the frame, and so on.

But for all their similarities, they are clearly different things. Better to call a motorcycle by its proper name so that we impart better understanding. Likewise, better to call something a Proxy instead of a Facade when appropriate.

Avoid Claiming A Special Context

When picking a context for a name, it is very tempting to define our own special context that lets us pick a name we prefer, perhaps for marketing or other non-technical reasons. We need to avoid this temptation.

Imagine if General Motors, in its marketing materials, called a "Kia" a "Maserati" and went on to describe its offerings in terms of its so-called Maserati. Such a description would make no sense in the wider world of automobile manufacture. Calling a "Kia" a "Maserati" while "in the context of GM" is symptomatic of the Humpty-Dumpty rationale:

"When I use a word," Humpty Dumpty said, in a rather scornful tone, "it means just what I choose it to mean -- neither more nor less." "The question is," said Alice, "whether you can make words mean so many different things." "The question is," said Humpty Dumpty, "which is to be master -- that's all."

Claiming your own special context adds a layer of unnecessary obfuscation and cognitive friction. It removes clarity in naming.


In summary, I opine that these are the guidelines we need to keep in mind when picking names:

  • We are writing software instructions, not prose or poetry.

  • Pick from existing names that provide clarity within the appropriate context.

  • The order of priority for the appropriate context is: programming language, software patterns, domain terms, and finally everyday speech.

  • When using a pattern name, find the best match, not one that is merely close-enough.

  • Avoid claiming a special context for names so that you can make them mean what you want, instead of what they mean within a more widely shared context.

UPDATE: Some commenters from the Laravel community are being, shall we say, uncivil in their statements. Comments that are not civil, polite, respectful, and on-topic will be deleted out of hand. I reserve the right to further police comments as I see fit.

Are you stuck with a legacy PHP application? You should buy my book because it gives you a step-by-step guide to improving you codebase, all while keeping it running the whole time.