Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should We Use Long-Name Or Short-Name in JavaScript Coding?

There is a discussion about JavaScript coding in my work group. Some people argues that we should use long-name for better readability; the others believes that short-name should be favored to same bits-on-wire.

Generally, it is about coding convention. One side believes that identifier such as "fAutoAdjustWidth" is OK, while others prefer to "fAtAjtW".

So, what's the better way? Should we sacrifice readability for performance or not?

like image 517
Morgan Cheng Avatar asked Mar 22 '09 13:03

Morgan Cheng


People also ask

What is the correct way of writing names in JavaScript?

Naming ConventionsAlways use the same naming convention for all your code. For example: Variable and function names written as camelCase. Global variables written in UPPERCASE (We don't, but it's quite common)

Is it okay to have long variable names?

Giving them longer names would only cause more confusion. If a variable is used frequently but represents something more complex, then you have to give it a clear name so that the user doesn't have to relearn what it means every time they use it.

Can function names be too long?

A function name is too long when it starts to either over-describe what it does, or when it hinders readability of the code. Well put. Keep the names clear and concise but still descriptive.


2 Answers

Make it readable, and if you feel that the resulting JS file is to big, use one of many JS compactors before deploying the production version, while maintaining development version with long names.

BTW. if your really worried about bandwidth, use mod_deflate.

like image 74
vartec Avatar answered Sep 23 '22 00:09

vartec


If you're worried about bits on the wire, you could always run a minifier on your code. Then you could develop with long names, and you could release with a much smaller file that has equivalent functionality. The Yahoo YUI Compressor looks like it does whitespace compression and token compression.

like image 27
rampion Avatar answered Sep 23 '22 00:09

rampion