DEV Community

Cover image for Naming: Every Developer's Nightmare

Naming: Every Developer's Nightmare

Samuel Braun on April 18, 2023

Aah.. naming things. A developer's favorite brain sport, nestled somewhere between attending endless meetings and refactoring code. As developers, ...
Collapse
 
sollyucko profile image
Solomon Ucko

I think ensureMinusAtStart would be a clearer name for the conditional version of addMinusAtStart.

Collapse
 
samuel-braun profile image
Samuel Braun

You're right! I wasn't sure if "ensure" was clear enough but hearing it from an independent source.. imma change it 👍

Collapse
 
jmccabe profile image
John McCabe

ensureMinusAtStart() is semantically correct if you are doing that, i.e. if you are adding a minus only if there is not one already there. In the case of:

val = startsWithMinusSign(valueOfCurrentField) ? 
            valueOfCurrentField : 
            ensureMinusAtStart(valueOfCurrentField);
Enter fullscreen mode Exit fullscreen mode

I would be inclined to retain an addMinusAtStart() function such that you have something like this (C++ kind of thing) :

std::string addMinusAtStart(std::string input) 
{
    const std::string minus { "-" } ;
    return minus + inputn
} 

std::string ensureMinusAtStart(std::string input) 
{
    const char minus { '-' };
    return ((input[0] == minus) ? input : addMinusAtStart(input));
} 
Enter fullscreen mode Exit fullscreen mode

Just a thought though.

Collapse
 
masterxilo profile image
Paul Frischknecht

having learned more about the importance of idempotency (aka desired state pattern) in (distributed) information systems, I use "ensure" all the time now. For example instead of delete(x), we might even write ensureDoesntExist(x).

Collapse
 
jmccabe profile image
John McCabe

You need to review your use of if (!valueOfOtherField) if you're targeting clarity. There's no evidence in your code that valueOfOtherField is a boolean type, but (based on C-type-derived languages) the ! operator takes a boolean as input and produces a boolean output.

It looks like you're relying on either an overload of the boolean ! operator, or an implicit cast of valueOfOtherField to a boolean type. Either case is not clear. I have no idea of the type returned by row.doc[otherField] so I would hope that a conditional that used that returned value would be a proper value comparison retuning a boolean (e.g., where the return type us a string, if (valueOfOtherField.isEmpty()).

Collapse
 
tythos profile image
Brian Kirkpatrick

Naming event listeners in particular, which can be asynchronously managed and therefore trickier to debug within context, is a great exercise. "on[Before|After][state][change]" is a good template here.

Collapse
 
samuel-braun profile image
Samuel Braun

Great, haven't thought of that one 👍 Thanks for sharing

Collapse
 
tythos profile image
Brian Kirkpatrick

Good references/examples in the careful thought put into the VS Code extensions API:

vscode-docs.readthedocs.io/en/stab...

Collapse
 
pcjmfranken profile image
Peter Franken

German "Gründlichkeit" at its finest. Very good!

Using logical scheme-based, objective descriptions saves lots of time and effort all around. This is especially helpful in large or long-lived projects, but, honestly, once you get used to naming things like this, you wouldn't have it any other way even in small one-off personal projects, either.

Just Do It meme

Collapse
 
brense profile image
Rense Bakker

Love the intro, more true words were never spoken xD

Collapse
 
adarshgoyal profile image
Adarsh Goyal

how to name a array which will hold two different data let say town data and city data based on condition.

I have named it cityTownData is there any better way to do that

Collapse
 
samuel-braun profile image
Samuel Braun • Edited

I did something similar a few weeks ago and needed to change it back. If you can, make them two seperate arrays as this becomes easier to scale and work with in the future. But if not, you could name it cityAndTownData to avoid confusion that "city" is just a descriptor for "town" and "cityTown" is one thing. The condition you can add at the end like this: cityAndTownDataWithLargePopulation or even better cityAndTownArrayWithLargePopulation or citiesAndTownsWithLargePopulation. "data" is too general and could be anything, so replacing it with "array" you give developers a better idea of the type. So final answer would be: citiesAndTownsWithLargePopulation

Collapse
 
adarshgoyal profile image
Adarsh Goyal

as it will be either city or town data so can i make it cityOrTownArray or should i keep them seprate ?

Thread Thread
 
samuel-braun profile image
Samuel Braun • Edited

Don't overengineer your variables and spend too much time thinking about one. The core purpose of good naming is so your code can be easily read by others and yourself without feeling stupid. If you feel stupid reading other peoples code its most likely their fault. Just check your entire function and ask yourself the question. Is the code as a whole clear and understandable? If yes youre fine.

PS: If cities and towns are two different things in your code, seperate them. If they are pretty much the same, just use one word, or a generalization word like "settlement".

Thread Thread
 
adarshgoyal profile image
Adarsh Goyal

ठीक है

Collapse
 
erinposting profile image
Erin Bensinger
Collapse
 
grimmdb profile image
GrimmDB

Great article of a fundamental subject.

Collapse
 
rcoswe profile image
rcoswe

This certainly makes sense to me. Naming is hard and having this pattern as a guide is really useful.

Collapse
 
webdynamics profile image
Vlado Petrushevski

Excellent points raised!

Collapse
 
tinaheiligers profile image
Christiane (Tina) Heiligers

Your tips are fantastic, thank you!

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
vadorequest profile image
Vadorequest

ChatGPT is my new best friend for naming things, no human can compare with all the (in)sanities it provides me!

Collapse
 
crusoexia profile image
Crusoe Xia

Why is naming hard? Because it is the abstraction itself.