Article
Your code will be a liability if your types have mediocre names
November 8, 2018
My journey towards creating code looks like this:
- Research the details of the goal (Learn)
- Come up with a plan to implement (Create)
- Code (Clicking and typing)
- Return to step 1 when I realize I had a false assumption (Regret)
Naming the types correctly is the final obstacle during planning before I can get into the (fun đ¤) labor of coding. Itâs like, ugh, I know the computer doesnât care what I name my type. Itâs all going to be stripped away by the time the machine is interpreting the instructions. My functionality wonât be affected. Why am I spending as much time naming as I did figuring out roles and how things fit together?
Because code isnât just for the machine. Itâs for developers who will have to understand it well enough to manipulate it correctly. âFuture youâ is included in that category.
To do this right requires clarity about your problem set, and enough empathy for your audience to anticipate all of the implications of your chosen name.
A True Name
Thereâs this old idea that if you know somethingâs â true name â then you have power over it. Itâs a common trope in fantasy â and part of religious stories.
Take the story of Isis and Ra . Isis was a great healer, but could only help Ra if she knew his real name. He tried to satisfy her request with âlesserâ names, but that only caused Ra to not get at the core issue. Once he gave up his true name, she was able to cure him.
That sounds like debugging to me. You canât fix the problem if you donât know the true meaning of the things youâre dealing with. Lazy labels will be deceiving. The more mental gymnastics youâre making people do while reading your code the more cognitively draining it will be to work with.
âYes thatâs called aProvider
but it actually just transforms data.ââThat modelId
isnât for this model, itâs for the other generic model.âBut if you truly know what something is you can manipulate it effortlessly. Thatâs important because code will have to be changed in the future. My ideal is absolute oneness between functionality and the label representing it. Since itâs an ideal, I never get there. There is such a thing as diminishing returns while thinking up names, but itâs worth putting in some time.
Balancing Clarity with Readability
The most precise name for something might beâŚ
That name blatantly ignores all available context. Obviously this example is ridiculous, but it can be tempting to add in unnecessary qualifiers.
Even if you feel the name isnât the best it could be, itâs better to be internally consistent within your project rather than waffle back and forth between two conventions. Once people are tuned into your projectâs standards, theyâll know what youâre talking about. A consistent message is better than a conflicted one, even if itâs ultimately subpar.
Also, donât be afraid to rename old elements when new elements are introduced, as a way to clarify their differences. Weâre not coding in stone tablets. Some people have a dream of future-proofing their code and anticipating future changes. Iâd rather make the code cleanly represent the present day situation, and make changes in the future that reflect the future. This saves me a lot of worrying about things outside of my control.
We Control Both Sides
Maybe youâre not taking the time to accurately name something because itâs doing multiple thingsâŚ
Sometimes the right answer might be to refactor the underlying implementation to enable a better name. We have control over both. Weâre not stuck trying to name something that canât be changed. We can rip things apart and be blunt.
Itâs common in politics to strive to get your chosen label onto an initiative, because all of the implications of your label will now be associated with said initiative. Something similar happens with your name, especially combined with the context of the project.
âAh, this was namedchosenAddress
as opposed to other one named address
â there must be some functionality about selecting an address without changing the official recordâŚââThis enum value is named notification
, while another is push
â whatâs the difference here? There must be a non-push notification, so is there polling? Maybe â or maybe I donât trust the developer now and Iâm going to dig into the code to see if theyâre using these terms correctly.âItâs worth the time investment to pause and properly name your types. Code isnât just for the computer, itâs for developers as well. By naming things properly, you can take advantage of that nameâs implications to guide your reader into understanding things correctly.
Sean never creates inappropriate acronyms in his code at Livefront .-
Sean Berry
Software Engineer