If you are writing a simple little loop, what should you name the counter?
Provide example loops!
The choice of a variable name should be mnemonic — that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.
In computer programming, a loop variable is a variable that is set in order to execute some iterations of a "for" loop or other live structure.
I always use a meaningful name unless it's a single-level loop and the variable has no meaning other than "the number of times I've been through this loop", in which case I use i
.
When using meaningful names:
It can be tricky to find the bug in this nested loop using single letters:
int values[MAX_ROWS][MAX_COLS]; int sum_of_all_values() { int i, j, total; total = 0; for (i = 0; i < MAX_COLS; i++) for (j = 0; j < MAX_ROWS; j++) total += values[i][j]; return total; }
whereas it is easier when using meaningful names:
int values[MAX_ROWS][MAX_COLS]; int sum_of_all_values() { int row_num, col_num, total; total = 0; for (row_num = 0; row_num < MAX_COLS; row_num++) for (col_num = 0; col_num < MAX_ROWS; col_num++) total += values[row_num][col_num]; return total; }
row_num
? - rejected alternativesIn response to some other answers and comments, these are some alternative suggestions to using row_num
and col_num
and why I choose not to use them:
r
and c
: This is slightly better than i
and j
. I would only consider using them if my organisation's standard were for single-letter variables to be integers, and also always to be the first letter of the equivalent descriptive name. The system would fall down if I had two variables in the function whose name began with "r", and readability would suffer even if other objects beginning with "r" appeared anywhere in the code.rr
and cc
: This looks weird to me, but I'm not used to a double-letter loop variable style. If it were the standard in my organisation then I imagine it would be slightly better than r
and c
.row
and col
: At first glance this seems more succinct than row_num
and col_num
, and just as descriptive. However, I would expect bare nouns like "row" and "column" to refer to structures, objects or pointers to these. If row
could mean either the row structure itself, or a row number, then confusion will result.iRow
and iCol
: This conveys extra information, since i
can mean it's a loop counter while Row
and Col
tell you what it's counting. However, I prefer to be able to read the code almost in English: row_num < MAX_COLS
reads as "the row number is less than the maximum (number of) columns";iRow < MAX_COLS
at best reads as "the integer loop counter for the row is less than the maximum (number of) columns".An alternative to row_num
I would accept is row_idx
: the word "index" uniquely refers to an array position, unless the application's domain is in database engine design, financial markets or similar.
My example above is as small as I could make it, and as such some people might not see the point in naming the variables descriptively since they can hold the whole function in their head in one go. In real code, however, the functions would be larger, and the logic more complex, so decent names become more important to aid readability and to avoid bugs.
In summary, my aim with all variable naming (not just loops) is to be completely unambiguous. If anybody reads any portion of my code and can't work out what a variable is for immediately, then I have failed.
1) For normal old style small loops - i, j, k - If you need more than 3 level nested loops, this means that either the algorithm is very specific and complex, or you should consider refactoring the code.
Java Example:
for(int i = 0; i < ElementsList.size(); i++) { Element element = ElementsList.get(i); someProcessing(element); .... }
2) For the new style java loops like for(Element element: ElementsList)
it is better to use normal meanigful name
Java Example:
for(Element element: ElementsList) { someProcessing(element); .... }
3) If it is possible with the language you use, convert the loop to use iterator
Java Iterator Example: click here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With