Re: For Loops and Variables
- From: Lew <lew@xxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 05 Apr 2007 17:54:42 -0400
Jason Cavett wrote:
On Apr 5, 3:49 pm, Eric Sosman <Eric.Sos...@xxxxxxx> wrote:Jason Cavett wrote On 04/05/07 11:47,:
This was a discussion my co-worker and I had awhile back and I was
curious what the group thought.
for(int i=0; i < someValue; i++) {
// do stuff
}
The discussion was that the use of the variable "i" was not good as it
should be named something that means something (arrayCounter or
whatever) so it is easier to understand the code. The reverse
argument was that using the "i" variable as a counter was an okay
practice and it is alright as long as it is the only variable doing
this (all other variables are well named). Convenience of typing "i"
versus "someLongVariableName" was the argument on this side.
As Oliver pointed out, 'i', 'j' and 'k' have been enshrined as loop index names since the 1960s. Anyone who doesn't feel comfortable with that deserves a job whose main function is asking, "Would you like fries with that?"
That said, I add the letter 'x' (for "index") to these just because I asses that one-character variables are a tad too easy to lose track of in the source text, thus 'ix', 'jx' and 'kx', respectively. I feel this makes them stand out better while still not straying too far from tradition.
It is also popular in Java to name the index variable 'index'.
--
Lew
.
- References:
- For Loops and Variables
- From: Jason Cavett
- Re: For Loops and Variables
- From: Eric Sosman
- Re: For Loops and Variables
- From: Jason Cavett
- For Loops and Variables
- Prev by Date: Re: Mutable Dimension, argh!
- Next by Date: Re: Problem applying generics to my code. Is there a better solution?
- Previous by thread: Re: For Loops and Variables
- Next by thread: Re: For Loops and Variables
- Index(es):