There is a commercial on TV these days where a little girl is helping her mother and grandmother prepare a large turkey for a holiday dinner. The little girl asks the older women why the turkey only has one leg. The other leg has been cut off and the older women explain that this is because the little girl’s great grandmother always did it that way. Then a flashback shows that the reason the leg was originally cut off was because the great grandmother didn’t have a pot that whole hold the turkey unless one leg was cut off first. Her daughter and granddaughter and great granddaughter could probably easy buy a pot large enough. In fact the pots they have now might be large enough. But “because that is the way we have always done it” these women were still cutting off one leg.
I remember Grace Hopper once saying in a talk that if we every said “because that is the way we have always done it” she would appear behind us and haunt us. I can’t hear the phrase without thinking of here over 30 years later. But in a real way that is why so many programmers using index variables of one letter and that the letter is between “i” and “n.” So what’s the story?
For many people back in the day the principal programming language was FORTRAN. Early versions of FORTRAN would let you specific the variable type of a name but the default values for integers were variables that started with the letters “i” through “n.” Also with some systems longer variable names took up more room in memory. And memory at that time was expensive. So to make it easy a lot of people used “i”, “j”, and “k” as their first choice for index variable names. That’s how I learned it.
Those of us who learned FORTRAN as a first language brought that habit with us to other languages. To BASIC, and C, and Java, and even PASCAL. And many many more. Why? “Because that is the way we have always done it” Shame on us. And the people we taught taught the same thing to others. That’s not really good. “index” is such a better variable name. In specific cases there are even more helpful, more descriptive and more useful variable names. For counting loops, for variables to pass to methods and many other places. Generally most of use have gone beyond the one character variable name. The last big sticking point seems to be index/loop variables. It’s probably time we all got past that. At least we should teach better practices to our students.
I switched to calling the variable counter a few years back, after a static analysis tool (I think it could've been FXCop) marked i as a bad variable name.
Maybe the use of good static analysis tools should be introduced into schools more heavily to help show learners even through it works, it may not be the best way?
This strikes me as a good thing rather than a bad thing. We preach about reusing code all the time, why not reuse coding conventions as well?
In other words, if everybody (well, most-everybody) is doing the same thing....let's keep doing it.
Just my opinion.
I agree with previous commenter Jamie.
Just because our current reasoning isn't the same as it was 30 years ago doesn't mean we should stop doing it. I like this convention when dealing with short loops - if I ever see "i", then I know I'm in a for loop, and if I see "j", then I know I'm nested inside another for loop where "i" is the "parent" loop.
If we didn't use "i", "j", etc., then there probably would be no convention - some people would use "counter", maybe "counter1" and "counter2" or "outerIndexer" and "innerIndexer" or other crazy stuff like that.
But at the same time, I would absolutely not use one-character variables for anything other than an indexer, ever. I'd never have a string named s, or a float named f, in an actual application.
I have to agree with jtenos. I like the i,j,k thing. I think programmers are inherently lazy (after all, they sit on their ass in front of a computer all day, how much lazier can you be) and having some little conventions like this allow me to be even lazier. As soon as the i,j,k become confusing they are toast.
I used to use meaningful names for loop variables but abandoned them.
I got sick of "so what will I call the next one?" - it just wasn't something useful to spend my time on.
the "i,j,k" convention is known and understood across a range of languages, and because of the context it is always clear that these are loop variables. If there is any doubt about the purpose of the loop, write a comment.
How about the advice:
"i, j, k" are your best choice for loop indexes except for when they are not.
Personally, I use "ii, jj, kk" and never have one letter variables. Regardless, as loop indexes one or two letter variables do not noticeably impact readability or maintainability of code.
I would have said that it was a habit carried over from math. Take the Captial Sigma notation for a summation, where you write stuff like n over Capital Sigma over i=m, meaning that the index i varies from m to n. Capital Sigma being hard to transcribe in simplel inear algorithmic languages, you transcribe that as FOR I=M TO N or for (i=m, i<n, i++) or whatever, but keep the simple index i.
I program mostly in Java these days, and generally follow Sun’s guidelines: http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367
I like short names for “temporary” variables.
I find myself becoming much less tolerant lately of “noise” in source code. Consider the following:
ArticulatedArmMember articulatedArmMember = new ArticulatedArmMember();
I consider all the repetition of “articulatedArmMember” to be noise, and much prefer something like:
ArticulatedArmMember aam = new ArticulatedArmMember();
But only for local variables where the scope is short. For an instance variable I would use the full name, articulatedArmMember.
Since we're admitting date, let's take it one step further. For those of us who cut our teeth on Fortran AND used mark sense cards, it not only made sense, but led to fewer errors when creating the program.
Of course, that doesn't explain why we didn't change and grow with it but that's my story and I'm sticking to it. i for the first loop; j for the next embed; then k ...
On the other hand, there's the old adage that if a program was hard to write, it should be equally as hard to modify by others!
Buy a bigger pot? Yeah, just put it on the credit card. Thinking like yours got us into this whole mess!
I've wondered about this before. I tried longer count variables but these got in the way and so I went back to i and j. I tell my classes to use i and j too, since they are quick and easy to use, and they will see this practice in other programs.
Whilst I can see a point in adding value to the names I don't think the value is worth the hassle. And anyway, why worry? One of my favourite quotes (which programmers should keep in mind) is "A foolish consistency is the hobgoblin of little minds" from Emerson.
While yes IDEs do make it faster to have longer names, but for looping, i,k,j just make sense. Yes, it was taught to me that way but there is no need IMO to make them any longer if all they are doing is acting as an index. If that index had additional calcuations done on it, that may be a different case.
Long story short, I agree with @jtenos
Using counter instead of i, is one of the stupidest things i've read that was not on the dailyWTF. Longer is not better. i is just as unique, and descriptive. If you want to write self documenting code then you might want to chose a long index variable name that describes what is being iterated through. For example for(int rowI = 0; rowI < nRows; rowI++) is usefull if you are iterating through say rows of SQL data.
Even there it isn't quite necessary. If you make sure to comment the actual statements in the for loop, any programmer worth his salt should be able to look at for(int i=0; i<nRows; i++) and realise that i is the index for rows.
I'll probably continue to use i,j,and k as I learned in 1972 (before Watfor...) because I only do it in short-scoped situations where the meaning is clear to the reader. It saves me typing, conforms to math notation for the most part, and doesn't hurt anything.
I still catch myself typing "Dimension i" instead of "Dim i".