开发者

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();
    开发者_如何学Go}
    
  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?


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.


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


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.


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.


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.


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

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜