Hacker News new | ask | show | jobs
by tmuir 3704 days ago
Even "i" can be changed to "index". My rule of thumb is this: If you wouldn't use the abbreviation when speaking, don't use it when coding.

I don't understand why 'i' has been given a pass. The argument boils down to saved keystrokes. Typing is not the bottleneck.

5 comments

> I don't understand why 'i' has been given a pass.

Because it is well enough established (in both programming and mathematics) that it communicates literally no less information to the reader than "iterator" or "index" would in the same context.

> The argument boils down to saved keystrokes.

Not at all. I, for one, find it easier to see the shape of the whole expression when the variable names are shorter.

Yes; prolixity yields clarity in programming about as much as in prose. The old quip needs an update: a modern programmer can write COBOL in any language.
My guess would be that it derives from the mathematical notation for sums.

But apart from that, I think using i,j,k as index variables in for-loop has become such a de-facto standard that I would only prefer long names such as "count" or "index" in situations where multiple indices may cause confusion. But then I'd probably use even more specific names than "count" or "index".

> I don't understand why 'i' has been given a pass.

The reason is that in FORTRAN variables starting with i, j, k..n were defined as integers.

Back in the day I learned fortran first, then basic. People that learned basic first often would use the letter 'a' as a loop index instead of 'i'

Now days I still use 'i' but often use indx instead because my editor doesn't highlight single letter variables easily.

I'm okay with "i" (like others said, it's a well-known shorthand for "iterator" or "index") but I often use "idx" because it can easily be combined with other words in nested loops (e.g. "rowIdx" and "columnIdx").
I agree, and I have calculated this a while ago this based on average number of lines a programmer writes in 8 hours. I don't have the actual numbers but unless you type slower than ~30 words per minute, speed it isn't a bottleneck.

I have been using index instead of i in while loops lately. It's surprisingly concise.

When I started to program in C I used "n" for the longest time, simple because it was quicker on to type on my first computer; "Next N" was a quick two taps on the N key. Guess the platform :-)
A ZX80, ZX81 or ZX Spectrum.