The Most Active and Friendliest
Affiliate Marketing Community Online!

“AdsEmpire”/  Direct Affiliate

5 rules of variable naming

michiganannd

New Member
affiliate
When I was at uni some 10 years back now, I remember one of my lecturers telling me not to make variable names too long because you would get "pain in your fingers".

Well, rule #1 goes against that advice:

1. Make your variable names long and descriptive
Visual Studio has IntelliSense, Eclipse has its own code completion, and I'm sure whatever IDE you're using can finish your variable names off for you, too. Using long names prevents the ambiguity of short or cryptic names.

2. Put units in your variable names
If you are writing an engineering application you are going to be using variables with units. Embed the unit name in the variable, for example, distanceInMM.

3. If you are using Camel Case, don't capitalise commonly hyphened, or combined words.
Let me explain.

Callback is normally spelt as one word. So, pretty please, don't call your variable callBack.

4. Never, ever use the variable name temp. The only perfectly valid exception to this rule, is when you're writing a swap function.

5. int i is perfectly valid in a small loop. I've met programmers who would crucify me for saying this, but when your loop is half a dozen lines of code long or less, int i is perfectly valid as a loop counter. It's so widely used, it's almost expected.

Source: drivensource.net/Tutorials/5-rules-of-variable-naming
 
Now you made me remember my college days :) that was really sweet:baloon:
and now I miss my friends :( anyway my professor taught as the same way, but I didn't know that it was the vice versa when it comes to SEO.. thanks for those tips informative :)
 
heh, my top 5 most favourite variables go as follows:

md (its an instance of my database class)
ii (use it in loops)
k (key)
v (value)
mj (my ajax)

using long descriptive variable names is not always desirable - for instance, in javascript, you have to be more careful over as you try to keep the code less in size and in memory usage - and you tend to get namespacing issues...

so, at some frameworks that are concious of this--for instance, YUI--you get to work with instances such as "YAHOO.test.Class2.superclass.testMethod.call(... "

i don't care what IDE you use, it will struggle to auto-suggest... then again, i once worked with a guy that claimed the average experienced programmer produces about 4 lines of _useful_ code a day (which is what is left after 10 coffees, a lot of planning, testing and refactoring). how cost effective it is to a business to retain such people on a contract is beyond me -- and no wonder, they spend half their time camelcasing their variables :D
 
banners
Back