Someone over at the MSDN blogs recently posted a mini-rant about people using inscrutable variable names in short apps…
He mentioned that some people use things like i, and others use random letters like x. Believe it or not, there is actually some structure and reasoning behind these choices of variable names, and a certain amount of historical convention to it.
Reasons for using i, j, k as loop variable names:
These are mathematical (or engineering) index variables. Typically you’ll see them used in matrices. i indicates the row, j the column, and k? Well, you get the idea.
Reasons for using x, y, z as loop variable names:
These are used when you’re iterating over space – x indicates going across a row of pixels / space, and y indicates going vertically down that row of pixels / space. z, of course, goes into the page.
The only other version I’ve seen is l,m,n – used on the Sinclair Spectrum, because hitting the L key the first time gives you the token for the BASIC FOR command, whereas hitting it the second time gives you L – which is handy as you can rationalize it as “loop variable”.
Using i, ii, iii is, however, incorrigible and encourages errors – any repeated single-letter name is going to give you trouble, as you can easily misread it.
Using i, ii, iii, iv, v, vi, vii, viii, ix, x, however, will lead to you being cudgeled to death with a sundial by a software engineer.