7

Possible Duplicate:
Why do most of us use 'i' as a loop counter variable?

I was just writing a nested loop and got to the point where I was using l and m as variables to control for loops, and I realized this could get very confusing; Ive already had a few bugs when I copied blocks of code to different levels. So I was thinking instead of using i,j,k I would use iSomething,iSomethingelse. So if I were going over a 3D model Id use

for(int iMesh=0;iMesh<numMesh;iMesh++)
    for(int iVertex=0;iVertex<meshes[iMesh].numVertex;iVertex++)

or maybe use the name of the variable Im iterating over:

for(int iMeshes

for(int iVerts

So I was wondering if there are any other standard or commonly used practices for doing this?

EDIT: Im wondering if there ARE any standards, not if its OK to do this

zacaj
  • 185

7 Answers7

18

i,j and k are the standard counter variables. By using them you imply the variables are used to keep loop count and nothing else. If you use another more complex name then its less clear what the variable is used for.

If your counter variables are getting confusing then its a sign your code needs breaking up. i.e

for(int i=0;i<numMesh;i++)
   foo += CountVerticies(meshes[i]);
Tom Squires
  • 17,835
7

for standard looping people pick i,j,k because it is easy and in a lot of situations you don't have a meaning for it so i,j,k is meaningful also known by all developer so it is pretty close to a standard.

anyway if you need to change to something more meaningful, use the following guidelines that i picked from uncle bob book "Clean Code" note there is other things also to consider from the book, but this is what i thought fit to the looping name: -

  1. Use Intention-Revealing Names
  2. Avoid Disinformation
  3. Make Meaningful Distinctions
  4. Use Pronounceable Names
  5. Use Searchable Names
  6. Avoid Encodings (Hungarian Notation,Member Prefixes)
  7. Don't be cute
  8. Pick One Word per Concept

so the code will end up something like this more or less

        for(int cellPointer = 0; cellPointer < 10;cellPointer++)
        {
            for (int rowPointer = 0; rowPointer < 10; rowPointer++)
            {
               //do something
            }
        }

so when you go back to it you can understand it, if somebody from outside got to work on it after you i'll be readable to him

omarqa
  • 452
2

Use i, j, k... if your indexes have no "meaning".

I would use mesh, vertex, etc. without an i prefix instead, unnecessary Hungarian is evil.

You could also use stuff such as i0, i1, ... , in if you had a great number of nested loops, but I think more than 3-4 is unusual.

alex
  • 2,914
2

x,y, and z can be useful, especially when dealing with 2D or 3D coordinates.

Sure beats trying to remember that i = x and j = y, etc

Neil N
  • 612
1

i is standard parlance in mathematics to represent any integer in a series:

1, 2, 3 ... i, i+1, i+2 ... n.

The i stands for Integer (according to one of my former professors, I have no other cite.) So, at any given time in the execution of your loop, it would be accurate from a mathematical perspective to call it i.

Jacob G
  • 191
0

One of the problems with using counter variables is that outside of the control logic for iteration they don't have any meaning.

Your instinct is correct that using iVertex is better than using i, as that adds clarity. However, the 'i' prefix tells you that the variable is an integer, which, while helpful in some circumstances isn't that useful with modern IDEs (this is one reason several answers argue against it).

What is best is just the general advice for naming a variable, which is based on what the variable contains. In this case, you're thinking of the variable based on how it is modified, but not on how it is used, or what it represents. A loop counter is incremented from some start to some end, but one can tell that by looking at the control structure. A better option is to name it based on how it is consumed by the body of the loop. In this case, iMesh is used as an index into the meshes array, so I would use something like 'meshId'.

One good reason for naming based on how something is intended to be used is that one can more easily detect code defects. For example, if I see "a = MeshId+2" I'm going to be very suspicious, because generally we don't want to be doing arithmetic operations on an Id, only index operations (meshes[MeshId]).

There are coding standards, but the problem is that there are too many of them. For further reading, you can try here:

http://en.wikipedia.org/wiki/Naming_convention_(programming)

Neal Tibrewala
  • 1,103
  • 7
  • 12
-2

Choose a name that describes what the variable is for, such as

for (int loopCounter; loopCounter < 10; loopcounter++)
{
     if( loopCounter == doSomethingVal)
     {
          //do somthing

This is the approach I use so that you can read out the logic of the code without having to translate the code to "english" which makes it easier for a new person to read the code and understand the intent of the code (as the code reads out the logic as would be spoken between people). A lot of people will not stylistaclly like this though (cue downvoting!) beacuse they do not like verbose code, or are just too comfortable with i,j,k, or are using a memory limited language/hardware. The problem with using somehing short like just i is that if you are debugging or reading a large section of code and you come to a like like:

   ......
   someVar = i;

then you need to look up what i is, what someVar is and what this line means in terms of what the program is attempting to model. But if you have something like:

  ......
  currentItemId = loopItemCounter;

then it is more obvious what the intent of the command is without comments or having to read through a large chunk of code. For short functions/methods it doenst make much difference (because you can probably see all the code on one page), but if you have ever tried to debug a large program with variable names that are just i or kk you will see why I prefer to be more vebose in nameing.

I think that I picked up this practice after reading the book code complete, or one of the other programming pricipals books.

adam f
  • 380