Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I always use a <strong>meaningful name</strong> 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 <code>i</code>.</p> <p>When using meaningful names:</p> <ul> <li>the code is more understandable to colleagues reading your code,</li> <li>it's easier to find bugs in the loop logic, and</li> <li>text searches for the variable name to return relevant pieces of code operating on the same data are more reliable.</li> </ul> <h2>Example - spot the bug</h2> <p>It can be tricky to find the bug in this nested loop using single letters:</p> <pre><code>int values[MAX_ROWS][MAX_COLS]; int sum_of_all_values() { int i, j, total; total = 0; for (i = 0; i &lt; MAX_COLS; i++) for (j = 0; j &lt; MAX_ROWS; j++) total += values[i][j]; return total; } </code></pre> <p>whereas it is easier when using meaningful names:</p> <pre><code>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 &lt; MAX_COLS; row_num++) for (col_num = 0; col_num &lt; MAX_ROWS; col_num++) total += values[row_num][col_num]; return total; } </code></pre> <h3>Why <code>row_num</code>? - rejected alternatives</h3> <p>In response to some other answers and comments, these are some alternative suggestions to using <code>row_num</code> and <code>col_num</code> and why I choose not to use them:</p> <ul> <li><strong><code>r</code></strong> and <strong><code>c</code></strong>: This is slightly better than <code>i</code> and <code>j</code>. 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.</li> <li><strong><code>rr</code></strong> and <strong><code>cc</code></strong>: 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 <code>r</code> and <code>c</code>.</li> <li><strong><code>row</code></strong> and <strong><code>col</code></strong>: At first glance this seems more succinct than <code>row_num</code> and <code>col_num</code>, and just as descriptive. However, I would expect bare nouns like "row" and "column" to refer to structures, objects or pointers to these. If <code>row</code> could mean <em>either</em> the row structure itself, <em>or</em> a row number, then confusion will result.</li> <li><strong><code>iRow</code></strong> and <strong><code>iCol</code></strong>: This conveys extra information, since <code>i</code> can mean it's a loop counter while <code>Row</code> and <code>Col</code> tell you what it's counting. However, I prefer to be able to read the code almost in English: <ul> <li><code>row_num &lt; MAX_COLS</code> reads as "the <strong>row num</strong>ber is <strong>less than</strong> the <strong>max</strong>imum (number of) <strong>col</strong>umn<b>s</b>";</li> <li><code>iRow &lt; MAX_COLS</code> at best reads as "the <strong>integer loop counter</strong> for the <strong>row</strong> is <strong>less than</strong> the <strong>max</strong>imum (number of) <strong>col</strong>umn<b>s</b>".</li> <li>It may be a personal thing but I prefer the first reading.</li> </ul></li> </ul> <p>An alternative to <code>row_num</code> I would accept is <code>row_idx</code>: the word "index" uniquely refers to an array position, unless the application's domain is in database engine design, financial markets or similar.</p> <p>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.</p> <p>In summary, my aim with all variable naming (not just loops) is to be <strong>completely unambiguous</strong>. If <em>anybody</em> reads any portion of my code and can't work out what a variable is for immediately, then I have failed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload