Tuesday, February 8, 2011

Is a variable named i unacceptable?

As far as variable naming conventions go, should iterators be named i or something more semantic like count? If you don't use i, why not? If you feel that i is acceptable, are there cases of iteration where it shouldn't be used?

  • It helps if you name it something that describes what it is looping through. But I usually just use i.

    From mrinject
  • i is definitely acceptable. Not sure what kind of justification I need to make -- but I do use it all of the time, and other very respected programmers do as well.

    Social validation, I guess :)

    From Ian P
  • Yes, in fact it's preferred since any programmer reading your code will understand that it's simply an iterator.

  • Take a look at http://stackoverflow.com/questions/101070/what-is-an-ideal-variable-naming-convention-for-loop-variables, which addresses this question.

    VirtuosiMedia : Nothing came up in the similar questions section when I was typing this, so I didn't see that question. Thanks for that, though.
    Jon Ericson : @VirtuosiMedia: That's ok. Now we have another keyword for people to search on.
  • As long as you are either using i to count loops, or part of an index that goes from 0 (or 1 depending on PL) to n, then I would say i is fine.

    Otherwise its probably easy to name i something meaningful it its more than just an index.

  • If the "something more semantic" is "iterator" then there is no reason not to use i; it is a well understood idiom.

  • "i" means "loop counter" to a programmer. There's nothing wrong with it.

  • I tend to use i, j, k for very localized loops (only exist for a short period in terms of number of source lines). For variables that exist over a larger source area, I tend to use more detailed names so I can see what they're for without searching back in the code.

    By the way, I think that the naming convention for these came from the early Fortran language where I was the first integer variable (A - H were floats)?

    VirtuosiMedia : That's an interesting little tidbit about the history. Thanks.
    Greg Rogers : as they say "God is real, unless declared integer"
    From paxdiablo
  • Here's another example of something that's perfectly okay:

    foreach (Product p in ProductList)
    {
        // Do something with p
    }
    
    Steve Jessop : Absolutely. Nobody ever complains that mathematical proofs which read, "consider a continuous function, f" are incomprehensible because the function is called "f" rather than "a_continuous_function".
    From Ian P
  • I should point out that i and j are also mathematical notation for matrix indices. And usually, you're looping over an array. So it makes sense.

  • Depends on the context I suppose. If you where looping through a set of Objects in some collection then it should be fairly obvious from the context what you are doing.

    for(int i = 0; i < 10; i++)
    {
        // i is well known here to be the index
        objectCollection[i].SomeProperty = someValue;
    }
    

    However if it is not immediately clear from the context what it is you are doing, or if you are making modifications to the index you should use a variable name that is more indicative of the usage.

    for(int currentRow = 0; currentRow < numRows; currentRow++)
    {
        for(int currentCol = 0; currentCol < numCols; currentCol++)
        {
            someTable[currentRow][currentCol] = someValue;
        }
    }
    
    Martin Beckett : I would use iRow and iCol, but then I started on FORTRAN
    Josh : FORTRAN is still awesome for scientific computing. Lol, scientists will actually laugh at you if you mention other languages.
    sixlettervariables : Double any single character variable to make searching useful. ii v. i, xx v. x.
    Outlaw Programmer : The 'double any single character' trick is less useful when you have an IDE with built in refactoring wizards. As for Josh's answer, this is basically a perfect example of what to do. Respeck!
    Orion Edwards : for a nested loop over a 2 dimensional array, the convention is to use i, then j. currentRow and currentCol are way too wordy :-) That said, I agree with the point, which is that if things are not clear, don't use i
    mat_geek : currentRow and currentCol could easily be called just row and col.
    Camilo Díaz : Orion Edwards is right. For the 2nd case, you'd better stick to using i, j.
    Dan Walker : In the first instance however, you would be better off using foreach (assuming the language supports it)
    Thomas Owens : For a two-d array that represents a grid, I often use x and y, which correspond to the concept of x and y axis.
    demoncodemonkey : agree with mat_geek about row & col
    From Josh
  • As long as you're using it temporarily inside a simple loop and it's obvious what you're doing, sure. That said, is there no other short word you can use instead?

    i is widely known as a loop iterator, so you're actually more likely to confuse maintenance programmers if you use it outside of a loop, but if you use something more descriptive (like filecounter), it makes code nicer.

    From Dan Udey
  • i is acceptable, for certain. However, I learned a tremendous amount one semester from a C++ teacher I had who refused code that did not have a descriptive name for every single variable. The simple act of naming everything descriptively forced me to think harder about my code, and I wrote better programs after that course, not from learning C++, but from learning to name everything. Code Complete has some good words on this same topic.

  • What is the value of using i instead of a more specific variable name? To save 1 second or 10 seconds or maybe, maybe, even 30 seconds of thinking and typing?

    What is the cost of using i? Maybe nothing. Maybe the code is so simple that using i is fine. But maybe, maybe, using i will force developers who come to this code in the future to have to think for a moment "what does i mean here?" They will have to think: "is it an index, a count, an offset, a flag?" They will have to think: "is this change safe, is it correct, will I be off by 1?"

    Using i saves time and intellectual effort when writing code but may end up costing more intellectual effort in the future, or perhaps even result in the inadvertent introduction of defects due to misunderstanding the code.

    Generally speaking, most software development is maintenance and extension, so the amount of time spent reading your code will vastly exceed the amount of time spent writing it.

    It's very easy to develop the habit of using meaningful names everywhere, and once you have that habit it takes only a few seconds more to write code with meaningful names, but then you have code which is easier to read, easier to understand, and more obviously correct.

    From Wedge
  • I use i for short loops.

    The reason it's OK is that I find it utterly implausible that someone could see a declaration of iterator type, with initializer, and then three lines later claim that it's not clear what the variable represents. They're just pretending, because they've decided that "meaningful variable names" must mean "long variable names".

    The reason I actually do it, is that I find that using something unrelated to the specific task at hand, and that I would only ever use in a small scope, saves me worrying that I might use a name that's misleading, or ambiguous, or will some day be useful for something else in the larger scope. The reason it's "i" rather than "q" or "count" is just convention borrowed from mathematics.

    I don't use i if:

    • The loop body is not small, or
    • the iterator does anything other than advance (or retreat) from the start of a range to the finish of the loop:

    i doesn't necessarily have to go in increments of 1 so long as the increment is consistent and clear, and of course might stop before the end of the iterand, but if it ever changes direction, or is unmodified by an iteration of the loop (including the devilish use of iterator.insertAfter() in a forward loop), I try to remember to use something different. This signals "this is not just a trivial loop variable, hence this may not be a trivial loop".

  • It depends. If you're iterating over some particular set of data then I think it makes more sense to use a descriptive name. (eg. filecounter as Dan suggested).

    However, if you're performing an arbitrary loop then i is acceptable. As one work mate described it to me - i is a convention that means "this variable is only ever modified by the for loop construct. If that's not true, don't use i"

  • The use of i, j, k for INTEGER loop counters goes back to the early days of FORTRAN.
    Personally I don't have a problem with them so long as they are INTEGER counts.
    But then I grew up on FORTRAN!

    From DaveF
  • i is fine, but something like this is not:

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            string s = datarow[i][j].ToString(); // or worse
        }
    }
    

    Very common for programmers to inadvertently swap the i and the j in the code, especially if they have bad eyesight or their Windows theme is "hotdog". This is always a "code smell" for me - it's kind of rare when this doesn't get screwed up.

  • i is so common that it is acceptable, even for people that love descriptive variable names.

    What is absolutely unacceptable (and a sin in my book) is using i,j, or k in any other context than as an integer index in a loop.... e.g.

    foreach(Input i in inputs)
    {
        Process(i);
    
    }
    
  • i think i is completely acceptable in for-loop situations. i have always found this to be pretty standard and never really run into interpretation issues when i is used in this instance. foreach-loops get a little trickier and i think really depends on your situation. i rarely if ever use i in foreach, only in for loops, as i find i to be too un-descriptive in these cases. for foreach i try to use an abbreviation of the object type being looped. e.g:

    foreach(DataRow dr in datatable.Rows)
    {
        //do stuff to/with datarow dr here
    }
    

    anyways, just my $0.02.

  • my feeling is that the concept of using a single letter is fine for "simple" loops, however, i learned to use double-letters a long time ago and it has worked out great.

    i asked a similar question last week and the following is part of my own answer:

    // recommended style              ●    // "typical" single-letter style
                                      ●
    for (ii=0; ii<10; ++ii) {         ●    for (i=0; i<10; ++i) {
        for (jj=0; jj<10; ++jj) {     ●        for (j=0; j<10; ++j) {
            mm[ii][jj] = ii * jj;     ●             m[i][j] = i * j;
        }                             ●        }
    }                                 ●    }
    in case the benefit isn't immediately obvious: searching through code for any single letter will find many things that aren't what you're looking for. the letter i occurs quite often in code where it isn't the variable you're looking for.

    i've been doing it this way for at least 10 years.

    note that plenty of people commented that either/both of the above are "ugly"...

    Jon Ericson : And for people with editors that can search on word breaks, completely pointless.
    Andrei Rinea : Offtopic a little : why do you preincrement the loop variable in the for statement? I usually see **POSTINCREMENT** instead of preincrement.
    just mike : efficiency. it comes from my early experience with the C language when the assembly optimizers weren't perfect. if you post-increment, the expressions is first evaluated, then incremented, then evaluated again. if you pre-increment, there's no initial evaluation, just incremented then evaluated.
    From just mike

0 comments:

Post a Comment