views:

2476

answers:

28

If you are writing a simple little loop, what should you name the counter?

Provide example loops!

+4  A: 

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.

just mike
They look quite unreadable to me, especially parallel-line letters (e.g., ii, ll, jj, etc.)
Jon Limjap
Besides, why do you need to search for loop variables when they shouldn't reside anywhere beyond the loop? Not unless you have tons of code in between -- which isn't a good idea.
Jon Limjap
If you need to search for your iteration-variables, then it's refactoring-time!
Magnar
Which is ironic Magnar, because he did say "simple little loop". LOL
Jon Limjap
Switch to an editor that lets you include word boundaries in search patterns, e.g. '\<i\>' or has a "whole word only" checkbox.
finnw
If your variables have meaning outside the loop, then don't call them anything that doesn't have meaning.If your loops are so large that you have to search for the variables within the loop, then your loop is too large. Refactore it to make it more readable!
Bill Michell
If the variable has no deeper sense (like "column" or "row" within an array for example) I use tripple letters, like iii, jjj, kkk.
koschi
That's just ugly!
dysfunctor
I agree with finnw. Visual studio .Net lets you right click on a variable and "see all references". Any good IDE should provide this functionality.
Kibbee
I agree with others that have said, if you need to search through code for loop variables it is time to refactor.
Mitch Wheat
from Jeff's blog entry: http://www.codinghorror.com/blog/archives/001172.html here's some interesting code that every single one of us is using right here on SO: http://refactormycode.com/codes/333-sanitize-html (see line 22) ;-)
just mike
Maybe a bit personal thing, but I really do not like this naming convention. I find it ugly, confusing and higly unreadable. -1
petr k.
+6  A: 

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.

Nescio
People may argue that naming index variables is pointless, but I find it keep you in the habit of giving conscious thought to variable naming. Besides, if you're using C, you need to make sure you avoid variable name clashes with any other loops in the same method.
Mal Ross
Mal, use C99 and declare your loop variables in the loop heading (assuming you can choose to use C99, of course).
Derek Park
Ooops. Thanks for the heads-up. It's clearly been a long time since I used a C compiler.
Mal Ross
+4  A: 

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.

Vaibhav
A: 

I've started using perlisms in php.

if its a singular iteration, $_ is a good name for those who know its use.

Kent Fredric
A: 

My habit is to use 't' - close to 'r' so it follows easily aftewr typing 'for'

Simon Munro
+4  A: 

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.

AnthonyWJones
+26  A: 

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

m_pGladiator
i certainly agree with @Ande Turner that "i" *is* self descriptive for 99.9% of programmers. however, you've made my point for me in your Java example... if you "refactor" your loop to not use "i" -- you can't do a global replace on the loop because of "someProcessing". you could if it was "ii" ;-)
just mike
@just mike: You can do a global replace: \bi\b
J.F. Sebastian
An interesting alternative I learned some years ago is to use "idx" (short for "index") and then, if you have a second or nested loop, use "jdx", "kdx" etc. So it continues the i,j,k tradition but is sort of readable.
mtruesdell
A: 

Like a previous poster, I also use ii, jj,.. mainly because in many fonts a single i looks very similar to 1.

Emile
why not use a different letter then? or a whole word?
Jon Limjap
or a different font!
slim
It's also much easier to search for / highlight "ii" or "jj" in an editor.
John Millikin
Once again, I would have to question why you need to search for loop variables. Unless you are coding complex (complicated as oppose to Complex!) mathematical algorithms, loop vars should have a very limited scope.
Mitch Wheat
Code is written once and read many many times. So it should be immediately clear what you read. For very small scope, a short "word" should be short but still distinguishable from a 1. I think ii en jj are good balance. For larger scopes I agree with the previous commenter and use a word.
Emile
+2  A: 

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.

Jez
I often use k to iterate over range and n or n_something to mark the size of the range. Otherwise I am complete agreement with you.
Antti Rasinen
I like that you used widget for each element in widgets
epochwolf
+1  A: 

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.

Learning
+23  A: 

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:

  • the code is more understandable to colleagues reading your code,
  • it's easier to find bugs in the loop logic, and
  • text searches for the variable name to return relevant pieces of code operating on the same data are more reliable.

Example - spot the bug

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;
}

Why row_num? - rejected alternatives

In 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".
    • It may be a personal thing but I prefer the first reading.

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.

Paul Stephenson
I find the first to be more readable. The longer names (especially the "_num" everywhere) just adds to the visual clutter.
Derek Park
I think your example it poor, because the problem is not with the index variables. The problem is with the loop conditions. If you're looking at the wrong piece of code (index names in this case), you're not going to find the problem, regardless of how you name things.
Derek Park
The problem is not with the index variables, but I think a new code reader is less likely to spot that there _is_ a bug in the first example. In the second, the expression "row_num < MAX_COLS" should definitely set alarm bells ringing, even with a casual browse through the code.
Paul Stephenson
Why not abbreviate row_num to r and col_num to c? The meaning of the variable is just as clear to a casual reader, and you don't have so much visual clutter to deal with.
dysfunctor
and of course even better would be: rr and cc! ;-)
just mike
This is not a relevant example, in my opinion. Moreover, the first example is much more readable and one is therefore much more likely to see the bug in there.
petr k.
I think Petr K and Derek haven't maintained other people's code very much. The second example is a lot easier to read and, more importantly, understand. Do some maintenance programming for a while and you'll long for meaningful variable names.
Onorio Catenacci
If you really want to include semantic information about the variable, typical convention is to do something like "iRow, iCol" just because absolutely everyone understands the convention that i is a looping variable.
Greg Rogers
or if it's a cell based row/column layout based maybe common index variables like x and y can be used. But I find the first more readable.
cynicalman
Onorio, if you need loop variables on small loops to be "meaningful", I think I don't want to maintain your code. Seriously, if you can't handle "i" and "j" as indexes for tiny loops, I fear your code. I'm all for meaningful names, but there's a difference between meaningful and verbose.
Derek Park
mattlant
Derek, of course with a single-level loop with a body of three lines there shouldn't be any confusion using "i". Doing this though you'd have to have a threshold for "complex enough to switch to meaningful names". I am now in the habit of using names all the time -- editor auto-completion helps!
Paul Stephenson
A: 

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.

Karthi
+1  A: 

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.

Davy Landman
+9  A: 

Examples: . . . In Java


Non-Iterative Loops:


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
}


Iterative Loops:


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.

_ande_turner_
A: 

Perl standard

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.

Jon Ericson
+1  A: 

@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.

_ande_turner_
+2  A: 

I use i, ii, iii, iv, v ... Never got higher than iii, though.

Oddmund
+1  A: 

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.

Tim Ottinger
i agree with everything you say (except i've come to learn that it's better to say "recommendation" than "rule") -- and most people think my variable names are too long."... do as your teammates do..." is one reason i used the "consistency" tag. another reason is of course "do as you yourself do".
just mike
+5  A: 

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.

petr k.
A: 

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.

Tim Gradwell
A: 

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

Midhat
A: 

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)
Ed L
In which case they are actually meaningful names
Nat
+1  A: 

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.

Derek Clegg
+2  A: 

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.

Onorio Catenacci
A: 

I usually use:

for(lcObject = 0; lcObject < Collection.length(); lcObject++)
{
   //do stuff
}
Riddari
...assuming of course that nothing adds or removes anything from the Collection!
Mitch Wheat
A: 

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.

the0ther
A: 

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.

loudej
A: 

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.

J.T. Hurley