tags:

views:

85

answers:

8

Let's say you have a piece of code where you have a for-loop, followed by another for-loop and so on... now, which one is preferable

  1. Give every counter variable the same name:

    for (int i = 0; i < someBound; i++) {
        doSomething();
    }
    
    
    for (int i = 0; i < anotherBound; i++) {
        doSomethingElse();
    }
    
  2. Give them different names:

    for (int i = 0; i < someBound; i++) {
        doSomething();
    }
    
    
    for (int j = 0; j < anotherBound; j++) {
        doSomethingElse();
    }
    

I think the second one would be somewhat more readable, on the other hand I'd use j,k and so on to name inner loops... what do you think?

+1  A: 

first one is good for me,. cz that would allow you to use j, k in your inner loops., and because you are resetting i = 0 in the second loop so there wont be any issues with old value being used

ovais.tariq
A: 
void doSomethingInALoop() {
  for (int i = 0; i < someBound; i++) {
    doSomething();
  }
}

void doSomethingElseInALoop() {
  for (int i = 0; i < anotherBound; i++) {
    doSomethingElse();
  }
}
Daniel Moura
+1  A: 

In a way you wrote your loops the counter is not supposed to be used outside the loop body. So there's nothing wrong about using the same variable names.

As for readability i, j, k are commonly used as variable names for counters. So it is even better to use them rather then pick the next letter over and over again.

Ihor Kaharlichenko
I would have to disagree, because `j` is used only a loop nested inside an outer `i` loop, and so on. If I see a `j`, I expect nesting.
Steven Sudit
A: 

If the loops are doing the same things (the loop control -- not the loop body, i.e. they are looping over the same array or same range), then I'd use the same variable.

If they are doing different things -- A different array, or whatever, then I'd use use different variables.

James Curran
A: 

So, on the one-hundredth loop, you'd name the variable "zzz"?

The question is really irrelevant since the variable is defined local to the for-loop. Some flavors of C, such as on OpenVMS, require using different names. Otherwise, it amounts to programmer's preference, unless the compiler restricts it.

Russ
I agree 100%, except that if you have more than 24 of them in one scope, you **really** need to refactor your code.
T.E.D.
Well, yeah: but the point is, who cares if you use the same one for each loop or use a different one for each loop? If it program compiles and meets the programming standards of the shop you're programming for, the question is completely irrelevant.
Russ
It's relevant to deciding what the programming standards *should* be, though.
Steven Sudit
+1  A: 

I find it interesting that so many people have different opinions on this. Personally I prefer the first method, if for no other reason then to keep j and k open. I could see why people would prefer the second one for readability, but I think any coder worth handing a project over to is going to be able to see what you're doing with the first situation.

Shaded
+4  A: 

I reuse the variable name in this case. The reason being that i is sort of international programmerese for "loop control variable whose name isn't really important". j is a bit less clear on that score, and once you have to start using k and beyond it gets kind of obscure.

One thing I should add is that when you use nested loops, you do have to go to j, k, and beyond. Of course if you have more than three nested loops, I'd highly suggest a bit of refactoring.

T.E.D.
j is perfectly clear as well, imho, but it is more commonly used for the second index.
ldigas
+1  A: 

The variable should be named something related to the operation or the boundary condition.

For example: 'indexOfPeople', 'activeConnections', or 'fileCount'.

If you are going to use 'i', 'j', and 'k', then reserve 'j' and 'k' for nested loops.

Thomas Langston
+1 - Variables should be named for what they mean. I wouldn't use i,j,k unless I was doing matrix multiplications or something like that.
Jeffrey L Whitledge
@Jeff: Sometimes you don't want to emphasize the irrelevant index variable that's scoped to the loop. Calling it `index` would also work, but may be giving it more time on stage than it deserves.
Steven Sudit
@Steven Sudit - Maybe so. I often use `index` for the purpose. My habits started back in the VB3 days when I had to maintain somebody else's software that had a lot of single-letter variables. Just try to find all the uses of the variable `i` in a twenty-thousand line program using an early 1990's IDE. `Dim x as integer` gives two false-positives, and that's just one line. It was impossible to be confident of how a variable was used back then if it was only one letter.
Jeffrey L Whitledge
@Jeff: If you use a regexp-supporting search, or have the option explicitly, you can have it find `i` only when it's a word in itself. Having said that, I'm not against using a longer name *if* it benefits the code. My concern is that the fact that it's an index is obvious regardless of its name, so fleshing it out doesn't add much value.
Steven Sudit
If the benefit of giving a descriptive name to an index is too low, it is probably a sign you should have used a collection and a foreach statement.
Thomas Langston