iii makes at least as much sense as k because you can see at a glance that you are three layers deep in nested loops. It is, in fact, slightly easier to recognize you're seeing three letter i's than it is to remember the relative distance of k from i in the English alphabet.
It's just that the convention and therefore the expectation is i, j, k, and so on.
If I'm going to defy the expectations of some of my readers, I will do so by making an even greater leap forward in readability which clarifies something about the role or nature of the iterator and loop.
Example
If, say, we have nested loops (three deep), but one is simply responsible for looping so many times — an arbitrary number of times, not the length of a collection, but just do this damn thing X times, I might use count, especially if it is meant to start at 1. Stay with me.
Then, say, the other two loops are iterating over a matrix representing a grid. Well, then they will likely get rowIndex and colIndex, or x and y if the function of the grid is more like a mathematical graph or a real-life map needing coordinates.
Now, at a glance even the most junior developers on my team will be able to cognitively separate the different functions of the three loops, just by reading the three for () lines — especially if the other variable in the loop conditional is well-named.
Contextually-named iterator variables can help prevent us from needing to read the body of a FOR loop while skimming code.
I'll type out longer names if it saves our developers cognitive load and maintenance time later on.
View all comments
Show parent comment
2
u/GreatDig Jun 06 '20
wow wtf