i is for index. j is simply the next letter and we're too lazy to think up something meaningful
Programmer Humor
Post funny things about programming here! (Or just rant about your favourite programming language.)
Rules:
- Posts must be relevant to programming, programmers, or computer science.
- No NSFW content.
- Jokes must be in good taste. No hate speech, bigotry, etc.
jndex
kndex
lndex
I always thought it stood for iterator
i2?
ii
A useful tip I picked up was to use ii
instead of j
for an inner loop. It's far more distinct than j
.
If for some terrible reason you have even more inner loops you can easily continue the trend i
, ii
, iii
, iiii
, iiiii
- or iv
, v
if you're feeling roman
If you have the need to nest 5 levels of for-loops, I suggest taking a step back and rethinking your approach, my friend.
Even if that other approach is just refactoring it into separate methods.
I just do i2, i3, etc
When you have multiple indices you're also bound to have multiple cardinals those indices count up to, say foo.length
and bar.length
, so foo_i
and bar_i
are perfectly legible and self-documenting. A bit Hungarian but Hungarian is good in small amounts. Unless you're dealing with width
and height
in which case it's x
and y
but it's not that width_i
would be incomprehensible.
Two or three "i"s is readable, but any more and you're counting.
I'Ve started using i, k, m, n that's usually enough.
At this point we might as well go full Roman as you suggested. MXMCIIV to MXMCCVII as indices.
It depends. x
and y
are either elements or coordinates, a
and b
usually elements though in e.g. Haskell reserved (by convention) for type variables.
The i
j
k
l
series is reserved for indices. n
m
etc. are the counts of something, as such you'll see i
counting up to n
. Both are due to mathematical sum notation and general mathematical convention. Random google result:
Let x~1~, x~2~, x~3~, …x~n~ denote a set of n numbers. x~1~ is the first number in the set. x~i~ represents the ith number in the set.
...if you're using a language in which you use i
often chances are you should stop coding in C and get yourself a language with iterators. Manual loops are a bug magnet.
People who name iterators with one letter have no soul.
two letters it is then
And people who iterate over 3D space using firstDimensionIndex, secondDimensionIndex, and thirdDimensionIndex instead of x, y, z have no sense 😜
x, y, and z are absolutely fine for spatial addressing.
I generally use a for each type loop or a map because I am usually applying some function across a collection, and in both cases I use the singular name from the collections plural.
’Cities.map(city -> …)’
For (val city in cities)
If I actually need the index for some reason I still prefer loop structures that give me the index and the item together
*note syntax pulled out of my head and not necessarily belonging to any specific language.
For ( city, index in cities)
cities.map((city, index) -> … )
If I need to double loop a matrix array I would use rowIndex and ColIndex for the indexes.
I find it hard to read when these are together:
- i, j, l
- n, m, u, v, w
From all the possible character combinations, somehow the lookalike combinations are among the most popular. Yes, probably comes from math. I hated it even more when my math prof's i and j on the board were indistinguishable.
It's my understanding that i,j are conventionally used in mathematics which carried over into programming, but specifically it comes from Fortran in which all integer variables start with "I" through "N" based on said mathematical convention
for <thing> in <amount> do...
I don't like i and j since they are commonly used for imaginary numbers. I like to start on n. Probably because I do DSP.
WTF, I have never used nor seen "j."
I don't usually have to name these variables these days though. Pretty much everything I use has foreach or some functional programming type stuff.
And like that, the off-by-one mistakes disappear.
j
is for a loop in a loop.
chuckles in Python
don't mind i
but personally always use index
or x, y, z
for games
Well. I guess I'm then a some kind of heretic then. 🤷
I started using the first letter of the thing I am iterating over. This is particularly helpful with nested loops so I can easily remember which index variable corresponds to which thing.
When my brain doesn’t work I’ll resort to naming them the single of the plural. Like keys turns into key when i don’t wanna call it “objkey” or “outrageouslylongnamethatmayormaynotbeafittongwordtodescribeakey”