helpermethod
helpermethod

Reputation: 62224

Naming the counter variables of consecutive for-loops

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?

Upvotes: 2

Views: 777

Answers (8)

T.E.D.
T.E.D.

Reputation: 44814

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.

Upvotes: 4

Thomas Langston
Thomas Langston

Reputation: 3735

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.

Upvotes: 1

Shaded
Shaded

Reputation: 17876

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.

Upvotes: 1

Russ
Russ

Reputation: 4173

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.

Upvotes: 0

James Curran
James Curran

Reputation: 103545

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.

Upvotes: 0

Ihor Kaharlichenko
Ihor Kaharlichenko

Reputation: 6270

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.

Upvotes: 1

Daniel Moura
Daniel Moura

Reputation: 7956

void doSomethingInALoop() {
  for (int i = 0; i < someBound; i++) {
    doSomething();
  }
}

void doSomethingElseInALoop() {
  for (int i = 0; i < anotherBound; i++) {
    doSomethingElse();
  }
}

Upvotes: 0

ovais.tariq
ovais.tariq

Reputation: 2625

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

Upvotes: 1

Related Questions