If you are writing a simple little loop, what should you name the counter?
Provide example loops!
If you are writing a simple little loop, what should you name the counter?
Provide example loops!
my experience is that most people use single letters, e.g.:
i
,
j
,
k
,
...
or
x
,
y
,
or
r
,
c
(for row/column)
or
w
,
h
(for width/height)
, etc.
but i learned a great alternative a long time ago, and have used it ever since: double letter variables.
// recommended style ● // "typical" single-letter style
●
for (ii=0; ii<10; ++ii) { ● for (i=0; i<10; ++i) {
for (jj=0; jj<10; ++jj) { ● for (j=0; j<10; ++j) {
mm[ii][jj] = ii * jj; ● m[i][j] = i * j;
} ● }
} ● }
in case the benefit isn't immediately obvious: searching through code for any single letter will find many things that aren't what you're looking for. the letter i
occurs quite often in code where it isn't the variable you're looking for.
Always try to name the variable something meaningful and in context.
If you cannot decide, then use "index", if only so that someone else (maybe you!) can more easily click on it for refactoring later.
Paul Stephenson See this answer for an example.
I use single letters only when the loop counter is an index. I like the thinking behind the double letter, but it makes the code quite unreadable.
I've started using perlisms in php.
if its a singular iteration, $_
is a good name for those who know its use.
My habit is to use 't' - close to 'r' so it follows easily aftewr typing 'for'
i
if I have a nested loop then also j
.
This convention is so common that if you manage to come across a variable i
in a block of code that you can't see the start of you still instantly recognise it for what it is.
1) For normal old style small loops - i, j, k - If you need more than 3 level nested loops, this means that either the algorithm is very specific and complex, either you should consider refactoring the code.
Java Example:
for(int i = 0; i < ElementsList.size(); i++) {
Element element = ElementsList.get(i);
someProcessing(element);
....
}
2) For the new style java loops like for(Element element: ElementsList)
it is better to use normal meanigful name
Java Example:
for(Element element: ElementsList) {
someProcessing(element);
....
}
3) If it is possible with the language you use, convert the loop to use iterator
Java Iterator Example: click here
Like a previous poster, I also use ii, jj,.. mainly because in many fonts a single i looks very similar to 1.
If the counter is to be used as an index to a container, I use i
, j
, k
.
If it is to be used to iterate over a range (or perform a set number of iterations), I often use n
. Though, if nesting is required I'll usually revert to i
, j
, k
.
In languages which provide a foreach
-style construct, I usually write like this:
foreach widget in widgets do
foo(widget)
end
I think some people will tell me off for naming widget
so similarly to widgets
, but I find it quite readable.
I use "counter" or "loop" as the variable name. Modern IDEs usually do the word completion , so longer variable names are not as tedious to use. Besides , to name the variable to its functionality makes it clear to the programmer who is going to maintain your code as to what your intentions were.
I always use a meaningful name unless it's a single-level loop and the variable has no meaning other than "the number of times I've been through this loop", in which case I use i
.
When using meaningful names:
It can be tricky to find the bug in this nested loop using single letters:
int values[MAX_ROWS][MAX_COLS];
int sum_of_all_values()
{
int i, j, total;
total = 0;
for (i = 0; i < MAX_COLS; i++)
for (j = 0; j < MAX_ROWS; j++)
total += values[i][j];
return total;
}
whereas it is easier when using meaningful names:
int values[MAX_ROWS][MAX_COLS];
int sum_of_all_values()
{
int row_num, col_num, total;
total = 0;
for (row_num = 0; row_num < MAX_COLS; row_num++)
for (col_num = 0; col_num < MAX_ROWS; col_num++)
total += values[row_num][col_num];
return total;
}
row_num
? - rejected alternativesIn response to some other answers and comments, these are some alternative suggestions to using row_num
and col_num
and why I choose not to use them:
r
and c
: This is slightly better than i
and j
. I would only consider using them if my organisation's standard were for single-letter variables to be integers, and also always to be the first letter of the equivalent descriptive name. The system would fall down if I had two variables in the function whose name began with "r", and readability would suffer even if other objects beginning with "r" appeared anywhere in the code.rr
and cc
: This looks weird to me, but I'm not used to a double-letter loop variable style. If it were the standard in my organisation then I imagine it would be slightly better than r
and c
.row
and col
: At first glance this seems more succinct than row_num
and col_num
, and just as descriptive. However, I would expect bare nouns like "row" and "column" to refer to structures, objects or pointers to these. If row
could mean either the row structure itself, or a row number, then confusion will result.iRow
and iCol
: This conveys extra information, since i
can mean it's a loop counter while Row
and Col
tell you what it's counting. However, I prefer to be able to read the code almost in English:
row_num < MAX_COLS
reads as "the row number is less than the maximum (number of) columns";iRow < MAX_COLS
at best reads as "the integer loop counter for the row is less than the maximum (number of) columns".An alternative to row_num
I would accept is row_idx
: the word "index" uniquely refers to an array position, unless the application's domain is in database engine design, financial markets or similar.
My example above is as small as I could make it, and as such some people might not see the point in naming the variables descriptively since they can hold the whole function in their head in one go. In real code, however, the functions would be larger, and the logic more complex, so decent names become more important to aid readability and to avoid bugs.
In summary, my aim with all variable naming (not just loops) is to be completely unambiguous. If anybody reads any portion of my code and can't work out what a variable is for immediately, then I have failed.
If it is a simple counter, I stick to using 'i' otherwise, have name that denotes the context. I tend to keep the variable length to 4. This is mainly from code reading point of view, writing is doesn't count as we have auto complete feature.
I have long used the i/j/k naming scheme. But recently I've started to adapt a more consequent naming method.
I allready named all my variables by its meaning, so why not name the loop variable in the same deterministic way.
As requested a few examples:
If you need to loop trough a item collection.
for (int currentItemIndex = 0; currentItemIndex < list.Length; currentItemIndex++)
{
...
}
But i try to avoid the normal for loops, because I tend to want the real item in the list and use that, not the actual position in the list. so instead of beginning the for block with a:
Item currentItem = list[currentItemIndex];
I try to use the foreach construct of the language. which transforms the.
for (int currentItemIndex = 0; currentItemIndex < list.Length; currentItemIndex++)
{
Item currentItem = list[currentItemIndex];
...
}
into
foreach (Item currentItem in list)
{
...
}
Which makes it easier to read because only the real meaning of the code is expressed (process the items in the list) and not the way we want to process the items (keep an index of the current item en increase it until it reaches the length of the list and thereby meaning the end of the item collection).
The only time I still use one letter variables is when I'm looping trough dimensions. But then I will use x, y and sometimes z.
Non-Nested Loops: . . . The Index is a value.
. . . using
i
, as you would in Algebra, is the most common practise . . .
for ( int i = 0; i < LOOP_LENGTH; i++ ) {
// LOOP_BODY
}
Nested Loops: . . . Differentiating Indices lends to comprehension.
. . . using a descriptive suffix . . .
for ( int iRow = 0; iRow < ROWS; iRow++ ) {
for ( int iColumn = 0; iColumn < COLUMNS; iColumn++ ) {
// LOOP_BODY
}
}
foreach
Loops: . . . An Object
needs a name.
. . . using a descriptive name . . .
for ( Object something : somethings ) {
// LOOP_BODY
}
for
Loops: . . . Iterators reference Objects. An Iterator it is neither; an Index, nor an Indice.
. . .
iter
abreviates an Iterators purpose . . .
for ( Iterator iter = collection.iterator(); iter.hasNext(); /* N/A */ ) {
Object object = iter.next();
// LOOP_BODY
}
while
Loops: . . . Limit the scope of the Iterator.
. . . commenting on the loops purpose . . .
/* LOOP_DESCRIPTION */ {
Iterator iter = collection.iterator();
while ( iter.hasNext() ) {
// LOOP_BODY
}
}
This last example reads badly without comments, thereby encouraging them. It's verbose perhaps, but useful in scope limiting loops in C.
In Perl, the standard variable name for an inner loop is $_. The for, foreach, and while statements default to this variable, so you don't need to declare it. Usually, $_ may be read like the neuter generic pronoun "it". So a fairly standard loop might look like:
foreach (@item){
$item_count{$_}++;
}
In English, that translates to:
For each item, increment it's item_count.
Even more common, however, is to not use a variable at all. Many Perl functions and operators default to $_:
for (@item){
print;
}
In English:
For [each] item, print [it].
This also is the standard for counters. (But counters are used far less often in Perl than in other languages such as C). So to print the squares of integers from 1 to 100:
for (1..100){
print "$_*$_\n";
}
Since only one loop can use the $_ variable, usually it's used in the inner-most loop. This usage matches the way English usually works:
For each car, look at each tire and check it's pressure.
In Perl:
foreach $car (@cars){
for (@{$car->{tires}}){
check_pressure($_);
}
}
As above, it's best to use longer, descriptive names in outer loops, since it can be hard to remember in a long block of code what a generic loop variable name really means.
Occasionally, it makes sense to use shorter, non-descriptive, generic names such as $i, $j, and $k, rather than $_ or a descriptive name. For instance, it's useful to match the variables use in a published algorithm, such as cross product.
@JustMike . . . A FEW C EXAMPLES: . . . to accompany the Java ones.
NON-NESTED loop: . . . limiting scope where possible
/*LOOP_DESCRIPTION*/ {
int i;
for (i = 0; i < LOOP_LENGTH; i++) {
// loop body
}
}
NESTED loop: . . . ditto
/*LOOP_DESCRIPTION*/ {
int row, column;
for (row = 0; row < ROWS; row++) {
for (column = 0; column < COLUMNS; column++) {
// loop body
}
}
}
One good thing about this layout is it reads badly without comments, thereby encouraging them.
It's verbose perhaps, but personally this is how I do loops in C.
Also: I did use "index" and "idx" when I started, but this usually got changed to "i" by my peers.
The first rule is that the length of the variable name should match the scope of the variable. The second rule is that meaningful names make bugs more shallow. The third rule is that if you feel like adding comment to a variable name, you chose the wrong variable name. The final rule is do as your teammates do, so long as it does not counteract the prior rules.
I use i, j, k (or r & c for row-column looping). If you need more than three loop variables in a method, the the method is probably too long and complex and your code would likely benefit from splitting the method up into more methods and naming them properly.
I've started to use context-relevant loop variable names mixed with hungarian.
When looping through rows, I'll use iRow
. When looping through columns I'll use iCol
. When looping through cars I'll use iCar
. You get the idea.
for numerical computations, matlab, and the likes of it, dont use i, j
these are reserved constants, but matlab wont complain.
My personal favs are
index first,second counter count
My favorite convention for looping over a matrix-like set is to use x+y as they are used in cartesian coordinates:
for x in width:
for y in height:
do_something_interesting(x,y)
Whatever you choose, use the same index consistently in your code wherever it has the same meaning. For example, to walk through an array, you can use i
, jj
, kappa
, whatever, but always do it the same way everywhere:
for (i = 0; i < count; i++) ...
The best practice is to make this part of the loop look the same throughout your code (including consistently using count
as the limit), so that it becomes an idiom that you can skip over mentally in order to focus on the meat of the code, the body of the loop.
Similarly, if you're walking through an 2d array of pixels, for example, you might write
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
...
Just do it the same way in every place that you write this type of loop.
You want your readers to be able to ignore the boring setup and see the brilliance of what you're doing in the actual loop.
Steve McConnell's Code Complete has, as usual, some excellent advice in this regard. The relevant pages (in the first edition anyway) are 340 and 341. Definitely advise anyone who's interested in improving their loop coding to give this a look. McConnell recommends meaningful loop counter names but people should read what he's got to say themselves rather than relying on my weak summary.
I usually use:
for(lcObject = 0; lcObject < Collection.length(); lcObject++)
{
//do stuff
}
i also use the double-letter convention. ii, jj, kk.
i think using those letters, even though they're doubled, is the best way to go. it's a familiar convention, even with the doubling.
there's a lot to say for sticking with conventions. it makes things a lot more readable.
For integers I use int index, unless it's nested then I use an Index suffix over what's being iterated like int groupIndex and int userIndex.
In Python, I use i, j, and k if I'm only counting times through. I use x, y, and z if the iteration count is being used as an index. If I'm actually generating a series of arguments, however, I'll use a meaningful name.