It depends.
x
andy
are either elements or coordinates,a
andb
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 seei
counting up ton
. Both are due to mathematical sum notation and general mathematical convention. Random google result:Let x1, x2, x3, …xn denote a set of n numbers. x1 is the first number in the set. xi 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.It’s a shame iterators in JS are trash for memory if you have a giant array
deleted by creator
i is for index. j is simply the next letter and we’re too lazy to think up something meaningful
jndex
kndex
lndex
I always thought it stood for iterator
I sometimes use it for “item”, knowing full well its established meaning as index or iterator, because I’m a rebel.
i2?
ii
When I’m doing coding interviews I always like to start off and say I’m a big fan of very long variable names. “As descriptive as you can be” I say. Then I get to my first for loop. Instead of i I use “iterator” and then when I start a nested loop I use “jiterator” and it always gets a laugh.
I used to conduct coding interviews at my old job. If someone came in and had some humor like that, it would be big bonus points in my book. Being someone I would like to be on a team with is very important. Plus, I think it shows confidence and being comfortable in situations that make most people nervous.
I’ve been at two start ups and they had me interview people. Honestly this is what I looked for. I’d ask basic questions to prove you had an idea about coding, but I can teach someone to code, I can’t teach someone to be someone I like working with.
You can teach them to code if there is an underlying level of logic to build off. I’ve met a few people in life who I know for a fact will never code, no matter how smart they generally are.
And even if it didn’t help my chances directly like that, even getting a small chuckle would help me be more comfortable and confident.
Honestly finding someone who can relax and intergrate into your team culture is arguably more important that anything
In old FORTAN variable starting with I…N are integers. This is how the practice began.
Learned that VERY recently from here, at NDC Oslo 2023, he mentioned it around 42:54. The whole talk is worth watching, its about the history of javascript all the way back to FORTRAN (the talk itself starts at 25:03).
Oh wow, I thought it was because “i” was a short way of writing “index”. Then “j” was just logical after that.
You are still correct! The letters I & n are the first letters on Index.
A useful tip I picked up was to use
ii
instead ofj
for an inner loop. It’s far more distinct thanj
.If for some terrible reason you have even more inner loops you can easily continue the trend
i
,ii
,iii
,iiii
,iiiii
- oriv
,v
if you’re feeling romanIf 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.
At this point we might as well go full Roman as you suggested. MXMCIIV to MXMCCVII as indices.
When you have multiple indices you’re also bound to have multiple cardinals those indices count up to, say
foo.length
andbar.length
, sofoo_i
andbar_i
are perfectly legible and self-documenting. A bit Hungarian but Hungarian is good in small amounts. Unless you’re dealing withwidth
andheight
in which case it’sx
andy
but it’s not thatwidth_i
would be incomprehensible.
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 a thin J, okay kid? Git gud, get on my level.
When the practice started, most (if not all) programming languages used capital letters. IIRC the computers that ran early FORTRAN (which is where the I,J,K, etc. convention comes from) didn’t even support lower case letters.
x is used for map, filter, etc. a and b are used for sorts, comparisons and merges. y might be used if I’m doing multiple lambda expressions (but that means I’m in a bad place already). I have no idea why, but these are firm rules in my brain.
I’ve gotten used to using the singular form as in…
records.filter((record) => …)
Not saying this way is better but it works for me.
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.
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
In fact this goes all the way back to Hamilton when he invented quaternion, in which i,j,k are used as basis vectors (which are generalizations of the imaginary i). Later Gibbs dropped the scalar component and gave us the modern vector.
I used starcraft references in mine till the project lead demanded I knock it off.
The protoss quotes were perfect.
I dunno why, but I have always used x, y, z for my generic for-loop variables.
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.
I always use x or y, coming from Python background
don’t mind
i
but personally always useindex
orx, y, z
for gamesdeleted by creator