开发者

Is it okay to leave the name for the variable in a for-loop as "i"? [duplicate]

开发者 https://www.devze.com 2023-02-15 09:42 出处:网络
This question already exists: Closed 11 years ago. 开发者_如何学运维 Possible Duplicate: Why are we using i as a counter in loops?
This question already exists: Closed 11 years ago. 开发者_如何学运维

Possible Duplicate:

Why are we using i as a counter in loops?

In Java, I commonly have snippets of code similar to:

for (int i = 0; i < someLimit; i++) {
// some relatively simple and short code to iterate
}

In fact, I rarely name the loop variable as anything other than "i" except for complex for-loops where I use the loop variable inside the loop repeatedly.

I'm accustomed to naming variables descriptively, but I haven't seen or been taught to name the for-loop variable.

Is leaving the for-loop variable named as "i" a bad practice?


If the code really is short and simple, then it could be OK.

I never do this, though. I learned to program with tools that did not have refactoring capabilities built in, and could not locate all variable references automatically. We had to search for variables using simple text searches. I had to maintain some code where they used nothing but single-letter variable names. That was such a nightmare, that I always use long unique variable names for everything now!

But with modern tools, single-letter variable names for indexers over short code segments is OK.


Yes, it is perfectly acceptable, and generally standard practice.

Standard practice is i, j, k for nested loops.

for(int i = 0; i < 5; i++)
{
    for(int j = 0; j < 5; j++)
    {
        for(int k = 0; k < 5; k++)
        {
            // do stuff
        } 
    }  
}

Beyond three nested loops, besides signifying some other issue, its left up to the programmer to define the inner loop rules.


My opinion - i is good for exactly this purpose. I use one-letter variables for short indexers and lambdas only, and I see most other developers do the same, so I'm with you on this.


In terms of code readability, pretty much everyone understands that i is the index. Giving it some other name almost makes it harder to read the code.

But I agree with you about complex situations. When you have nested loops, it can be hard to keep the meaning of, say, i, j and k in your head while you read the code. So, giving those indexes more helpful names can be helpful.

In my opinion, there is no reason other than readability for choosing the names of these variables.


IMHO it is quite ugly especially in nested loops. For example:

for (int i; i < someLimit; i++)
{
     for (int z; z < someLimit; z++)
     {
        ... // array{i, z}
     }
}


The i usually stands for index. So, if you'd like a tad more descriptive naming, use the full word. The full extent of variable names may not always, and by itself, provide a more readable code. Consider hungarian notation to really see how trying to make things more readable can backfire. I, for one, use two letters to state the type of varable (uc, ul, si, s, lp, etc), followed by capital first letter of the variable name, and abbreviate if words are longer than 5 chars.

So your i, in my writting, might be siInd.


Since Java 5, you might find you rarely need to use the index of your loop iteration. Take a look: http://download.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

for ( Object o : Collection<Object>)
{
    // do something
}

A lot cleaner.

0

精彩评论

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