Should we use a long name or short name in JavaScript coding?

My team discusses JavaScript coding. Some people argue that we should use a long name for better readability; others suggest that the short name should be approved for the same bit-by-wire.

This is usually a coding convention. One side believes that an identifier such as “fAutoAdjustWidth” is fine, while others prefer “fAtAjtW”.

So what is the best way? Should we sacrifice readability for performance or not?

+6
performance javascript coding-style readability
source share
12 answers

Make it readable, and if you feel that the resulting JS file will be large, use one of the many JS compactors before deploying the production version, supporting the development version with long names.

BTW. if you are really concerned about bandwidth, use mod_deflate .

+21
source share

If you are worried about bits on the wire, you can always run minifier in your code. Then you could develop with long names, and you could release with a much smaller file that has equivalent functionality. Yahoo YUI Compressor looks like it compresses in the form of spaces and tokens.

+8
source share

Don't these same people recommend writing comments in their code? Be extremely clear and descriptive with variable names.

+3
source share

while others prefer "fAtAjtW"

Even if bit-by-wire was a problem (which is not the case), a naming convention like this will make the code completely invisible after the first week of working on the project.

Reading the code will be practically impossible, and when writing code, people will have to constantly think about things like "was" fAutoAdjustWidth "abbreviated as" fAtAjtW "or" fAutAtW "?". This is a huge mental tax that you have to pay when writing code, which will lead to much lower performance.

Also, the problem is compounded by the fact that in Javascript you get a new variable for every erroneous name!

+3
source share

Use long names that are enough to describe variables and functions well.

One of the reasons you really need short names is to reduce the file size, but you can do this with tools when downloading on the Internet.

+2
source share

Perhaps you should not worry about the bits on the wire, but about the overhead of reading and re-viewing the code.

I prefer to use short names inside the function and create function names as long as necessary, but as short as possible, without losing any useful meaning.

No doubt, this is a compromise. It depends on whether you want your code to look like a natural language or to be more implicit and compact.

Some names of prefix variables for inserting contextual information into them. I say, if necessary, the IDE should provide injection capabilities such as a visual overlay of code through contextual symbology.

The next version of Visual Studio will make such an annotation gymnastics much easier thanks to the fine-grained extensibility mechanism, expanded deep in the editor itself. I have not used Visual Studio to edit Javascript.

Now I see that your concern is, rather, a space compromise. This should never be a problem. Readability over bits on a wire is always always always recommended, especially. since there is compression, as other commentators note.

The only thing I would like to add is that sometimes understanding is easier with compact names over excessively long names. But it is more correct to name short names. Long names are much easier and faster to do this first hand.

Short names should never be caused by data compression only by cognitive performance. What works individually.

+2
source share

Use the names of large variables because they help the programmer.

To save bits over a wire, first reduce your Javascript before deploying it to the production server. The Dean Edwards packer has the ability to compress variable names that look like the best of both worlds to you.

+2
source share

I would strongly recommend using short identifiers. Just by reading your example, you will find out how much additional documentation is needed when using names such as fAtAjtW. At some point, it will become almost invisible, and this is just to save some bytes for transmission.

If the only reason to consider “short” names is to make the resulting script smaller and thus save some bandwidth, I would recommend using gzip compression instead, which will save you more than a few bytes for the identifier.

+1
source share

One side believes that an identifier such as “fAutoAdjustWidth” is fine, while others prefer “fAtAjtW”.

'fAtAjtW - unreadable, indestructible horror. Seriously, does anyone prefer this? Fun and impossible to remember - is it "AtAjt" or "AutAdj ...?

'autoAdjustWidth would be the appropriate fully qualified attribute name. (I'm not at all sure of the notation with the prefix f, but this is another problem.) Sometimes you need a very short name for a short-lived variable (like a temporary one in a small loop), in which case I'd personally go straight to "var aaw "and not to the aforementioned nightmare.

As for performance, there will be no difference. JavaScript doesn't care how long you enter variable names and assume that you dump your scripts along the way to the browser, compression will remove all the benefits of passing shorter names.

+1
source share

Anyone who believes that "fAtAjtW" is preferred is using some kind of pharmacological method in his program. fAutoAdjustWidth is very thin and very circumspect. Javascript libraries do not use names like fAtAjtW, for some reason. If you are worried about size, then your worries are probably inappropriate. However, I recommend using some kind of minifier. However, said, do not use ridiculously long; probably something over 25-30 characters goes a little bit.

+1
source share

Use smaller names where this does not affect the readability of your code. Big names are beautiful, but try to use them only where it really makes it easier for you and others. Finally (and as indicated in other answers) minify your code and / or enable some kind of server compression mechanism such as apache mod_gzip or mod_deflate to reduce the number of bits passing through the wires.

With that said, I would give priority to readability by the compactness of variable names.

0
source share

Long and descriptive names.

And try to make as unique methods as possible. It helps shipping. If you want to find all the ways to use a particular method, the likelihood that you will encounter another method with the same name will be less likely.

Modern Javascript IDEs can also refactor methods (see http://blue-walrus.com/2013/08/review-javascript-ides/ ). It is very difficult if the methods are called the same.

0
source share

All Articles