Why I no longer pluralize my variables
Clean code reads like well-written prose — Uncle Bob
For the longest time, I pluralized variables to indicate multiples of something.
const user = new User();
const users = [user];
By pluralizing, I can ensure that my code reads like well-written prose. Thus, the following code reads almost like English.
users.find(user => user.name === 'Anbin');
Over the years, I have become increasingly convinced that pluralizing is the wrong way to name a variable.
An Inconsistent Language
English is not a language, it’s three languages wearing a trench coat pretending to be one. — Some Random Person on The Internet
English has many inconsistencies, including how plurals are handled.
For example, “Fish” doesn’t have a plural form, and “Scissors” doesn’t have a singular form.
Also, the singular form “Bacteria” is “Bacterium”, but for “Criteria” it’s “Criterion”.
When encountering this situation in code, many of us simply pluralize it by force and move on.
const fish = new Fish();
const fishes = [fish];
Honestly — why isn’t the plural form of fish is fishes?
Context Matters
const person = new Person();
const people = [person];
Initially, I assumed “People” is the only plural form of a “Person”, but a random elevator taught me that “Persons” are equally valid.
As I dug a little deeper, I found that it was proposed that we use “Persons” if we are using a person as a countable noun and “People” if we are using it as an uncountable noun.
Nowadays, “Persons” are used mostly in legal contexts and when deliberately referring to humans individually rather than collectively.
Personally, I believe “Persons” is the correct pluralization in a coding context.
What are we describing?
By the way, I am not suggesting we should start using collective nouns. While murder may be a perfectly acceptable way to describe crows in literature, I think it would do no good in programming.
Instead, I decided to look at it as describing a single object versus a collection of objects.
const someObject = new SomeObject()
const someObjectCollection = [someObject];
A collection properly encapsulates multiples of anything without the nuances of pluralization. So you no longer have to call a collection of fish “fishes” in your code.
Personally, I just use “List” instead since it’s shorter and gets the job done for me.
const someObject = new SomeObject()
const someObjectList = [someObject];
Aren’t we just adding types to the variable name?
In Java, a Collection is an interface that describes a generic group of items, while a List describes an ordered group of items.
In cases like these, appending a list or collection at the end may feel like adding type information to the variable, but I would argue that is not the case.
Not Hungarian Notation
Firstly, the rule came about to retire the Hungarian notation. Hungarian notation was first introduced when types were not a thing, and you were responsible for knowing how many bytes a variable was supposed to take.
Prefixing “b” for a single-byte variable and “sz” zero-terminated strings greatly helped keep your memory safe.
With the advent of modern programming, the language and compiler take care of all of this for you, and you no longer need a prefix to guide future code dwellers.
Primitive Types vs Reference Types
Secondly, primitive types are different from reference types, and we use reference types in variable names all the time.
Person person = new Person();
List<Person> personList = new ArrayList<Person>();
If the first line is correct, then the second line is equally correct. What we don’t want to see is the following.
// Don't do this
boolean emptyBoolean = false;
// Do this instead
boolean isEmpty = false;
What about a collection of collections?
Let’s say you are building the next-generation AI-powered SaaS for a jewelry store, and they typically have collections. There may be the “Tiffany T” collection or the “Jean Schlumberger by Tiffany” collection.
const jewelryCollection = new JewelryCollection();
const jewelryCollectionCollection = [jewelryCollection];
If I were to be honest, pluralization would have been better in this particular case.
const jewelryCollection = new JewelryCollection();
const jewelryCollections = [jewelryCollection];
Unfortunately — there is never a universally perfect solution so you will have to pick your compromise. This is a compromise I am willing to make.
Does it really matter?
Honestly, in the grand scheme of things, it doesn’t really matter if you choose to use pluralization or not. The occasional workarounds for pluralization quirks may be rare occurrences in the first place. So, while I strongly prefer non-pluralized variable names, this is not the hill I wish to die upon.
For instance, even in YoPrint, we continue using pluralization for our database table names. Since we started with pluralization, we decided to keep going with it for the sake of consistency. If I were to start over, I would just use a singular name for the database table instead.
Closing Thoughts
At the end of the day, I am fully aware I might be alone in this preference. I love the simplicity, uniformity, and clarity that non-pluralized naming convention provides.
I don’t know about you, there aren’t many “fishes” in my sea in the future.