What is an ideal variable naming convention for loop variables? [closed]

Solution 1:

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:

  • the code is more understandable to colleagues reading your code,
  • it's easier to find bugs in the loop logic, and
  • text searches for the variable name to return relevant pieces of code operating on the same data are more reliable.

Example - spot the bug

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;
}

Why row_num? - rejected alternatives

In 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".
    • It may be a personal thing but I prefer the first reading.

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.

Solution 2:

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