DEV Community

Zohar Peled
Zohar Peled

Posted on

Abbreviations in code: yay or nay?

Personally, I dislike abbreviations in code.

Whenever I see something like regno, mgr, dept in code I'm dealing with and stuff like that I pause for a second and consider renaming it.
If that's a code that I'm going to be in charge of in the future, I rename it on the spot. However, if I'm only helping out a fellow developer I have to consider their own likes and dislikes - because it's not my code to change.

So, do you Abb. or never Abbreviate?

Top comments (13)

Collapse
 
moopet profile image
Ben Sinclair

I steer clear of it unless it's an already existing convention in a project.

I've seen too many codebases with inconsistent names like _txt vs. _text, and really, if you think in 2019 that you're doing good work by saving one character in something that's almost certainly been autocompleted for you then you're missing the point.

Some people like being terse for the sake of being terse, perhaps because it makes them feel more clever.

I don't abbreviate without good reason, and if my variable name looks like it's going to be a 50-character sentence, I refactor.

Collapse
 
msamgan profile image
Mohammed Samgan Khan • Edited

I completely agree with ben, how much time can you save by skipping an "e" from a "user".

a variable named "user" sounds more clear than "usr".

Collapse
 
curtisfenner profile image
Curtis Fenner

Changes like abbreviate to abb are bad because there is no "standard" way to alleviate. Whatever you might save in typing/reading speed from going over a shorter word, you lose in the cognitive effort of matching the shortened version to an actual word.

IDEs (or advanced text editors) obviate most of the need to abbreviate -- you have auto complete and can find all references to a name with a click.

At a certain point, though, things can get too wordy, and you can establish a standard abbreviation, and usually use that form (eg ctx for context) -- but only do this if the word comes up a lot unavoidably, but doesn't mean anything.

Collapse
 
dan1ve profile image
Daniel Veihelmann

Abbreviated variable names are a super common anti-pattern.
It makes it harder to understand the code, especially for other devs that try to understand the code.

(Obvious exemptions are conventions, e.g. loop variables like "i" or caught exceptions "e")

Collapse
 
vergeev profile image
Pavel Vergeev

I vote abbreviate, but only a limited number of words, such as HTTP, JWT, SMTP.

Everyone has auto-completion these days, why bother with short names.

Collapse
 
peledzohar profile image
Zohar Peled

Your examples are acronyms, not abbreviations. Of course no one is expecting to see a variable name like hyperTextTransferProtocol, but would you use dept. instead of department?

Collapse
 
vergeev profile image
Pavel Vergeev

Oh, I didn't know the distinction. Thank you!

Then no, I would not like to see abbreviation in my code. This is for the sake of consistency and readability.

Thread Thread
 
peledzohar profile image
Zohar Peled • Edited

Well, as it turns out, acronyms are a specific type of abbreviations, so we where both correct. You might say that acronym is derived from abbreviation.
:-)

Collapse
 
scrabill profile image
Shannon Crabill

I do not abbreviate. I haven't come across a case in programming where it has been beneficial (saving a few characters in a small project seems silly).

On the corporate side, I do abbreviate in naming conventions for templates, etc, since there is usually a hard character limit. Something like 2019 Net Promoter Score Survey Email Initial Email might become NPS_Survey_EM_1.

Collapse
 
jdforsythe profile image
Jeremy Forsythe • Edited

It really depends on the word and the context.

If the word has a well-known abbreviation (such as dept) then it can be okay, but I only typically abbreviate if I'm in an anonymous function or short-lived scope. The shortness of the variable name hints at the short life of the variable. In other words, I didn't take much time writing the name because it will be gone soon.

However for something that doesn't have a well known abbreviation, I typically use the first letter in these scenarios. And if a variable is meant to live longer or be more important, it gets a full name.

I will agree that it may take an extra few milliseconds if you want to know the name of the thing, but the name of variables in these contexts is of much less importance and it is more apparent to the reader that the variable is of less importance than the others around it which can't be done if you write full names for every variable.

Of course there are exceptions to every rule. If convention says to use ctx, like with Koa, then you use ctx.

Collapse
 
jeastham1993 profile image
James Eastham

I never abbreviate, I think having easy to read code is much more valuable than saving characters.

Even if it only takes a millisecond to process, 'usrMgr' is so much worse that 'userManager'.

Collapse
 
varjmes profile image
james

Unless it's a variable used across the project, and it has a convention: no, I never abbreviate. Comprehension > Saving a vowel.

Collapse
 
valentinprgnd profile image
Valentin Prugnaud 🦊

I try to avoid abbreviations in code and in general. In my experience, they almost always lead to misunderstandings and/or slows down communication/onboarding/etc. in a company