Why Naming is #1 Skill for Writing Clean Code đ§Œđ§âđ»
Martin Ć oĆĄiÄ
Posted on October 11, 2023
In stories, you will often find the motif of a powerful demon that can be controlled only by knowing its true name. Once the hero finds out that name, through cunning dialogue or by investigating ancient tomes, they can turn things around and banish the demon!
I firmly believe writing code is not much different: through finding good names for functions, variables, and other constructs, we truly recognize the essence of the problem we are solving. The consequence of clarity gained is not just good names but also cleaner code and improved architecture.
I would go as far as to say that 90% of writing clean code is âjustâ naming things correctly.
Sounds simple, but it is really not!
Letâs take a look at a couple of examples.
Example #1
// Given first and last name of a person, returns the
// demographic statistics for all matching people.
async function demo (a, b) {
const c = await users(a, b);
return [
avg(c.map(a => a.info[0])),
median(c.map(a => a.info[1]))
];
}
What is wrong with this code?
- The name of the function
demo
is very vague: it could stand for âdemolishâ, or as in âgiving a demo/presentationâ, ⊠. - Names
a
,b
, andc
are completely uninformative. -
a
is reused in lambda inside themap
, shadowing thea
that is a function argument, confusing the reader and making it easier to make a mistake when modifying the code in the future and reference the wrong variable. - The returned object doesnât have any info about what it contains, instead, you need to be careful about the order of its elements when using it later.
- The name of the field
.info
in the result of a call tousers()
function gives us no information as to what it contains, which is made further worse by its elements being accessed by their position, also hiding any information about them and making our code prone to silently work wrong if their ordering changes.
Letâs fix it:
async function fetchDemographicStatsForFirstAndLastName (
firstName, lastName
) {
const users = await fetchUsersByFirstAndLastName(
firstName, lastName
);
return {
averageAge: avg(users.map(u => u.stats.age)),
medianSalary: median(users.map(u => u.stats.salary))
};
}
What did we do?
-
The name of the function now exactly reflects what it does, no more no less.
fetch
in the name even indicates it does some IO (input/output, in this case fetching from the database), which can be good to know since IO is relatively slow/expensive compared to pure code. -
We made other names informative enough: not too much, not too little.
- Notice how we used the name
users
for fetched users, and not something longer likeusersWithSpecifiedFirstAndLastName
orfetchedUsers
: there is no need for a longer name, as this variable is very local, short-lived, and there is enough context around it to make it clear what it is about. -
Inside lambda, we went with a single-letter name,
u
, which might seem like bad practice. But, here, it is perfect: this variable is extremely short-lived, and it is clear from context what it stands for. Also, we picked specifically the letteru
for a reason, as it is the first letter ofuser
, therefore making that connection obvious.
- Notice how we used the name
-
We named values in the object that we return:
averageAge
andmedianSalary
. Now any code that will use our function wonât need to rely on the ordering of items in the result, and also will be easy and informative to read.
Finally, notice how there is no comment above the function anymore. The thing is, the comment is not needed anymore: it is all clear from the function name and arguments!
Example 2
// Find a free machine and use it, or create a new machine
// if needed. Then on that machine, set up the new worker
// with the given Docker image and setup cmd. Finally,
// start executing a job on that worker and return its id.
async function getJobId (
machineType, machineRegion,
workerDockerImage, workerSetupCmd,
jobDescription
) {
...
}
In this example, we are ignoring the implementation details and will focus just on getting the name and arguments right.
What is wrong with this code?
-
The function name is hiding a lot of details about what it is doing. It doesnât mention at all that we have to procure the machine or set up the worker, or that function will result in the creation of a job that will continue executing somewhere in the background. Instead, it gives a feeling that we are doing something simple, due to the verb
get
: we are just obtaining an id of an already existing job. Imagine seeing a call to this function somewhere in the code:getJobId(...)
â you are not expecting it to take long or do all of the stuff that it really does, which is bad.
Ok, this sounds easy to fix, letâs give it a better name!
async function procureFreeMachineAndSetUpTheDockerWorkerThenStartExecutingTheJob (
machineType, machineRegion,
workerDockerImage, workerSetupCmd,
jobDescription
) {
...
}
Uff, that is one long and complicated name. But the truth is, that we canât really make it shorter without losing valuable information about what this function does and what we can expect from it. Therefore, we are stuck, we canât find a better name! What now?
The thing is, you can't give a good name if you don't have clean code behind it. So a bad name is not just a naming mishap, but often also an indicator of problematic code behind it, a failure in design. Code so problematic, that you donât even know what to name it â there is no straightforward name to give to it, because it is not a straightforward code!
In our case, the problem is that this function is trying to do too much at once. A long name and many arguments are indicators of this, although these can be okay in some situations. Stronger indicators are the usage of words âandâ and âthenâ in the name, as well as argument names that can be grouped by prefixes (machine
, worker
).
The solution here is to clean up the code by breaking down the function into multiple smaller functions:
async function procureFreeMachine (type, region) { ... }
async function setUpDockerWorker (machineId, dockerImage, setupCmd) { ... }
async function startExecutingJob (workerId, jobDescription) { ... }
What is a good name?
But letâs take a step back - what is a bad name, and what is a good name? What does that mean, how do we recognize them?
Good name doesnât misdirect, doesnât omit, and doesnât assume.
A good name should give you a good idea about what the variable contains or function does. A good name will tell you all there is to know or will tell you enough to know where to look next. It will not let you guess, or wonder. It will not misguide you. A good name is obvious, and expected. It is consistent. Not overly creative. It will not assume context or knowledge that the reader is not likely to have.
Also, context is king: you canât evaluate the name without the context in which it is read. verifyOrganizationChainCredentials
could be a terrible name or a great name. a
could be a great name or a terrible name. It depends on the story, the surroundings, on the problem the code is solving. Names tell a story, and they need to fit together like a story.
Examples of famous bad names
-
JavaScript
- I was the victim of this bad naming myself: my parents bought me a book about JavaScript while I wanted to learn Java.
-
HTTP Authorization header
- It is named
Authorization
, but is used for authentication! And those are not the same: authentication is about identifying yourself, and authorization is about granting permissions. More about it can be found here: https://stackoverflow.com/questions/30062024/why-is-the-http-header-for-authentication-called-authorization .
- It is named
-
Wasp-lang:
- This one is my fault: Wasp is a full-stack JS web framework that uses a custom config language as only a small part of its codebase, but I put
-lang
in the name and scared a lot of people away because they thought it was a whole new general programming language!
- This one is my fault: Wasp is a full-stack JS web framework that uses a custom config language as only a small part of its codebase, but I put
Support us! đâïž
To help us improve our name at Wasp-lang đ, consider giving us a star on Github! Everything we do at Wasp is open source, and your support helps us make web development easier and motivates us to write more articles like this one.
How to come up with a good name
Donât give a name, find it
The best advice is maybe not to give a name, but instead to find out a name. You shouldnât be making up an original name, as if you are naming a pet or a child; you are instead looking for the essence of the thing you are naming, and the name should present itself based on it. If you donât like the name you discovered, it means you donât like the thing you are naming, and you should change that thing by improving the design of your code (as we did in the example #2).
Things to look out for when figuring out a name
- First, make sure it is not a bad name :). Remember: donât misdirect, donât omit, donât assume.
- Make it reflect what it represents. Find the essence of it, capture it in the name. Name is still ugly? Improve the code. You have also other things to help you here â type signature, and comments. But those come secondary.
- Make it play nicely with the other names around it. It should have a clear relation to them - be in the same âworldâ. It should be similar to similar stuff, opposite to opposite stuff. It should make a story together with other names around it. It should take into account the context it is in.
- Length follows the scope. In general, the shorter-lived the name is, and the smaller its scope is, the shorter the name can/should be, and vice versa. This is why it can be ok to use one-letter variables in short lambda functions. If not sure, go for the longer name.
-
Stick to the terminology you use in the codebase. If you so far used the term
server
, donât for no reason start using the termbackend
instead. Also, if you useserver
as a term, you likely shouldn't go withfrontend
: instead, you will likely want to useclient
, which is a term more closely related to theserver
. -
Stick to the conventions you use in the codebase. Examples of some of the conventions that I often use in my codebases:
- prefix
is
when the variable is Bool (e.g.isAuthEnabled
) - prefix
ensure
for the functions that are idempotent, that will do something (e.g allocate a resource) only if it hasnât been set up so far (e.g.ensureServerIsRunning
).
- prefix
The simple technique for figuring out a name every time
If you are ever having trouble coming up with a name, do the following:
- Write a comment above the function/variable where you describe what it is, in human language, as if you were describing it to your colleague. It might be one sentence or multiple sentences. This is the essence of what your function/variable does, what it is.
- Now, you take the role of the sculptor, and you chisel at and shape that description of your function/variable until you get a name, by taking pieces of it away. You stop when you feel that one more hit of your imagined chisel at it would take too much away.
- Is your name still too complex/confusing? If that is so, that means that the code behind is too complex, and should be reorganized! Go refactor it.
- Ok, all done â you have a nice name!
- That comment above the function/variable? Remove everything from it that is now captured in the name + arguments + type signature. If you can remove the whole comment, great. Sometimes you canât, because some stuff canât be captured in the name (e.g. certain assumptions, explanations, examples, âŠ), and that is also okay. But donât repeat in the comment what you can say in the name instead. Comments are a necessary evil and are here to capture knowledge that you canât capture in your names and/or types.
Donât get overly stuck on always figuring out the perfect name at the get-go â it is okay to do multiple iterations of your code, with both your code and name improving with each iteration.
Reviewing code with naming in mind
Once you start thinking a lot about naming, you will see how it will change your code review process: focus shifts from looking at implementation details to looking at names first.
When I am doing a code review, there is one predominant thought I will be thinking about: âIs this name clear?â. From there, the whole review evolves and results in clean code.
Inspecting a name is a single point of pressure, that untangles the whole mess behind it. Search for bad names, and you will sooner or later uncover the bad code if there is some.
Further reading
If you havenât yet read it, I would recommend reading the book Clean Code by Robert Martin. It has a great chapter on naming and also goes much further on how to write code that you and others will enjoy reading and maintaining.
Posted on October 11, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.