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.
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 ijkl series is reserved for indices. nm 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 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.
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
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.
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.
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
Yep, this is the answer. In Fortran, all variables are assumed to be floats, unless the variable starts with I, J, K, L, M or N. I’m sure they had a good reason, but it sounds so bizarre today!
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.
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).
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.
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.
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.
I prefer index variable names that are two words. The second word is always 'index' and the first word describes the enumerable objects.
carIndex, productIndex, thingIndex
I'm not paid by the character count. Longer and more descriptive is better. Long lines that go past your 1080p monitor are probably not long because of variable names but because you insist on doing many things in one line (quit doin' that). For small functions this isn't necessary, but too often I'm shunted to the middle of a big function with two or three indecies doing acrobatics over one another and while working on it I have to constantly remind myself that this i and j mean particular things.
I have had too many times where I have been confused trying to figure out a giant nested loop because the writer used i/j/k or x/y/z. It’s even worse when they confused when a particular bug is because they confused what their single letter variables were and used j somewhere instead of i and no one caught it because it is so easy to brush over. Name your stuff what it is, make your life easier, make others lives easier.
I like range-based for loops. You can just name the iterator after the object that it actually is. Have to be a little careful though, if the container is named a plural noun, and the natural name is the same word minus the easy-to-miss 's'.
I is short for "index" for a traditional for loop for mapping over an array and looking up by index. J comes after I and is used for nested loops so it doesn't shadow the outer I.
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'm honestly prefer short but (usually) complete words. Somewhere along the line I realized that being explicit really helps when you need to change it later.
due to convention everybody understands what i and j are, I don't think they need longer names. If it's something more complicated than a counter or index then maybe you should be using a foreach loop instead (if language supports it)
I generally use 'count' for a counter and 'idx' for index.
I'm not using C or Java languages though - if I were I would probably go with the more classic terse approach.
Also, if I'm reviewing a PR and I have to load more of the diff context to understand what a variable represents, then that variable has the wrong name.
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”
It was very common in text books when showing nested loops
int nWhatTheCount = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < i; j++) {
for (int k = 0; k < j; k++) {
for (int l = 0; l < k; l++) { // and on, and on
nWhatTheCount++;
}
}
}
}
foreach is useful when you don't need to know the index of something. If you do, conventional i, j, k, etc. are useful.
A lot of it depends what you're doing (number crunching, for instance) or if you're in a limited programming language (why won't BASIC die already?) where parallel arrays are still a thing.