Naming things properly, completely, and clearly is a huge part of writing good code.ย And if you avoid these traps, itโs easy.
There is an old joke in programming circles:ย
There are two hard things in programming:ย cache invalidation, naming things, and off-by-one errors.
The first is truly hard, the third is the joke, but the second one?ย That one baffles me.ย Naming things is easy, or at least it should be.ย But for reasons unknown, we make it hard.ย Just call it what it is or what it does.ย But for some reason, we developers have an aversion to doing that.ย Here are some thoughts about why naming is hard for developers, along with some ideas for doing it right.
Nine reasons for bad naming
- We assume everyone knows what we know
- Meaning drift
- Sheer laziness
- Too eager to abbreviate
- Forgetting functions are verbs
- Inconsistency
- Going negative
- Prefixing
- Blah words
We assume everyone knows what we know
This is the most common reason that we name things badly.ย I know that EmpNo stands for EmployeeNumber, and that EmployeeNumber is the unique ID in the database. Why wouldnโt everyone else?ย Well, because the next poor developer who comes along might think that the EmployeeNumber is the number assigned to the employee for logging in, and has nothing to do with unique values in the database.ย
If EmployeeNumber is the unique ID in the database, why not just call it that?ย How about calling it EmployeeUniqueIDInDatabase?ย Sure, itโs a bit of a mouthful, but no one will ever mistake it for something else, right?ย
And if you say to me, โNick, thatโs too much typing!โ Iโll give you my standard response:ย Lazy is no way to go through life, my friend. Plus, these days, your IDE will do all of that typing for you. A long, clear, explanatory name is always superior to an abbreviation you think is obvious but really isnโt.ย
Meaning drift
Sometimes the meaning of a name can be less precise than it might be, and that meaning can drift over time.ย You might start out with a method called SaveReceipt that puts a copy of the receipt in the database.ย But over time, you may add printing to the routine, and move the actual saving to a different method, and suddenly your name is lying to you.ย Naming it SaveReceiptToTheDatabase in the first place might make that harder to happen.
Sheer laziness
While naming things isnโt hard, it does take a bit of thought.ย I guess some folks just donโt want to take the time to think about naming things.ย Iโm not even going to talk about how silly it is to use a single letter for a variable name.ย The only exception Iโll quarter is using i as the variable in a loop. (But Iโll argue vehemently that Index is better.)
Otherwise, give a variable a really good, full name.ย Sure, it may take some effort, but if you stop and ask, โWhat, exactly, is this thing?โ and then name it based on what your answer is, youโll have a great name.ย For instance, if you feel the need to do this:
If (EmployeeNumber > 0) and (OrderNumber > 0) {
// ...
}
Donโt be afraid to go the extra mile:
EmployeeIsValid = EmployeeUniqueIDInDatabase > 0;
ThereIsAnOrder = OrderNumber > 0;
ItIsOkayToProcessTheOrder := EmployeeIsValid and ThereIsAnOrder;
If ItIsOkayToProcessTheOrder {
// ...
}
That is massively more readable, and the variable names clearly explain what they represent. It would be very hard to confuse what is happening there, and it reduces the cognitive load of the next developer, who no longer has to parse complex boolean statements.ย
Too eager to abbreviate
Laziness isnโt good, but neither is being in a hurry.ย Being in a hurry might cause us to abbreviate things when there is no need to do so.ย Remember, the IDE will do a lot of typing for you.ย You might think youโre saving .876 seconds by typing acctBlnc instead of accountBalance, but really youโre just stealing precious hours from the poor guy maintaining your code.ย
And while we are at it, whose account balance is that?ย The companyโs?ย The customerโs?ย Who knows?
Why developers are afraid of long names is a mystery to me. Donโt abbreviate anything unless it is an industry standard like URL or HTTP.ย Again, just type it out.
Forgetting functions are verbs
All methods should be named as verbs and should completely describe what they do.ย getCustomer is good, but where are you getting the customer from?ย What, exactly, are you getting?ย getCustomerInstanceFromDatabase is better.ย Again, if you ask yourself, โWhat is this function doing?โ and then just name it based on your complete answer, youโll have more maintainable code.
Inconsistency
Itโs easy to be inconsistent.ย For instance, if you use the word Customer to mean a person standing in front of the point-of-sale system buying something, then make sure that is what you call them everywhere in the system.ย Donโt use the word Client to describe them, ever.ย Donโt call them Buyer in some other module.ย Use the same term for the same thing consistently, throughout your repository.
Going negative
As I mentioned a couple weeks ago, keep names positive, particularly Booleans (if you must use them).ย Names like isNotValid and denyAccess become abominations. For example:
if (!IsNotValid) or (!denyAccess) {
ย // ...
}
There is a reason we donโt use double negatives in the English language. You also should avoid them in your code.
Prefixing
Back in the day, Hungarian notation was all the rage.ย All names had a prefix that defined what they were in addition to the name.ย This has gone out of vogue, as it got complex.ย I prefer that the name be expressive enough to allow the maintainer to surmise what it is.ย For instance, EmployeeCount is obviously an integer, and FirstName is obviously a string.
Some people like to prefix their variable names with a letter to indicate the role that a variable plays in a methodโfor example, โlโ for local variables, โaโ for method arguments or parameters. and so on.ย I frown on this kind of thing. If your methods are so big that you canโt tell at a glance what role a variable is playing, then you need to refactor.ย
Blah words
Another thing to avoid is using words that have no real meaning but seem important somehow. Avoid words like Helper, Handler, Service, Util, Process, Info, Data, Task, Stuff, or Object. These are โblah words,โ or naming junk foodโempty words that serve no purpose.ย What method do you write that isnโt helpful?ย Which ones donโt handle something?ย What in your app isnโt actually data?ย How much code do you write that doesnโt perform a task?
Naming is easy
Naming things is easy, or it should be.ย Like many things in life, it can all go well if you avoid doing bad things more than worrying about doing good things.ย Good naming dramatically reduces the cognitive load when maintaining code. By making the code clearer, it helps developers avoid mistakes and introducing bugs.ย The mere seconds it takes to stop and think about a name can save literally hours down the road.
Coding for clarity and maintainability is always the way to go.ย Naming things properly, completely, and clearly is a huge part of writing good code.ย Besides, to paraphrase the famous saying, โName things like the person maintaining your code is a sleep-deprived psychopath with your home address.โ


