Notes on Naming Variables and More

Choosing names for variables, functions and other entities is a subtle skill. A good name is succinct and suggestive but not misleading. The verboseness and style of a good name depends on its context. The context includes its lexical scope but also the project’s audience. As if that was not hard enough, names are susceptible to bike shedding.

Photo by rawpixel.com on Unsplash

Experience helps in coming up with good names. So does having some rough rules. Since we care about being economic with our time the goal is to quickly come up with a good enough name. Indeed, names are important because they communicate intent. But they need not be perfect to be meaningful.

The most important rule is: The more local an entity the shorter its name can be. The reason is straightforward. There is enough context in the surrounding code section to derive the entity’s role and intent. In the same vein, static typing and an IDE allow you to use shorter names as they provide extra context. Another, at a first glance counter-intuitive, rule is: Use a more generic name if a more specific name does not capture the intent completely. It is better to be vague than misleading.

Jacob Gabrielson wrote a wonderful article that pretty much sums up my position with illustrative examples: A Guide to Naming Variables. Another great article is Bob Nystrom’s Long Names Are Long. And this Reddit comment thread shows that opinions about the right name can differ with good arguments on both sides.