Possible Duplicates:
Is a variable named i unacceptable?
What is an ideal variable naming convention for loop variables?
Coming from a C background I've always used int i
for generic loop variables. Of course in big nested loops or other complex things I may use a descriptive name but which one had you rather see?
int i;
for(i=0;i<Controls.Count;i++){
DoStuff(Controls[i]);
}
or
int index;
for(index=0;index<Controls.Count;index++){
DoStuff(Controls[index]);
}
In the current project I am working on there are both of these styles and index being replaced by ndx
.
Which one is better? Is the i
variable too generic? Also what about the other C style names? i, j, k
Should all of these be replaced by actual descriptive variables?
I use single-letter variables as counters a lot. Here is how I loop, exhaustively use the letters in the alphabet:
The scope of an item's visibility should dictate how descriptive that name needs to be.
If you literally have a tiny loop, i, j, and k are fine and typical index counters. Sometimes a more descriptive name can help illuminate intent, but if the for loop is set up like the following, then a more descriptive name doesn't really matter.
That said, abbreviations should never be used unless they're used consistently. I personally think
ndx
is a terrible identifier because it's hard to type; I can type English perfectly well and my programming speed is not limited by my typing speed. If you want to sayindex
sayindex
.I believe that it was in The Pragmatic Programmer that they said you shouldn't use abbreviations because then people will never know what abbreviation to use. I know I want a thing called
index
so I typeindex
but I get a compiler error. Now what? (Hunt through the code to find that it's writtenndx
will bother me.)As I try to think about it, about the only abbreviation that I use that isn't game specific is 'num' to stand in for 'numberOf'. Other than that I use 'npc' to mean non-player character, 'ai' to mean artificial intelligence, etc., etc., and sometimes I use abbreviations in small blocks, e.g. a 10-line function operating on a Camera may just call it 'cam', but the scope is small so it's easy to see what's going on and the possibility for confusion is limited.
So- small scope -> do whatever you like (as long as there's some consistency). Large scope -> make your names unambiguous, meaningful, and easy to type. (By "easy to type" I mean "easy to remember how to spell" as well as "don't go overboard".)
Short variable names are great, but they should have small scopes. And they should honor the conventions of the language. Until the day I day, my Haskell and ML code will have function-valued variables
f
,g
, andh
and monadic computationsm
, variables of unnknown typea
andb
, and lists of unknown element typesas
andbs
. But the scopes of these variables will be limited to short functions orwhere
clauses.My C code will have variables called
i
,j
,p
,q
,s
, andt
. But the scopes of these variables will be confined to individual loops (all praise C99 and the C++ backports!) or short functions. Whether it's a loop index or another variable, something that appears in a large scope gets a longer name.When possible, I'd favor descriptive names, since we should be striving for readable code.
For temporary variables that are used in a tight code block, a short variable name is acceptable.
But if the loop or code block is very long, it is better to have a longer descriptive variable name, if for no other reason than it will make doing text searches for it easier.
If it is small block, and there are no nested for loops, i is just fine, it's almost as unwritten rule that i is a loop incremental variable whenever it appears. In everything more complex, good naming is crucial.
Personally I use i,j however instead of loop variables I tend to try and use for each when the language allows it - I find that even better.