## Tuesday, May 28, 2019

 Example C++ code

The above fragment is from [1]. I never write loops like this. I use $$n$$ for limits or counts, but never for a loop index.

Looking at this, I realized I have many of these "rules". Such as:

1. $$x$$, $$y$$, $$z$$, and $$v$$, $$w$$ are always double precision variables. (I used to subtract points if a student would write
for (int x=0; ... ).
2. $$i$$, $$j$$, $$k$$ and $$m$$, $$n$$ are always integer variables.
3. Never use $$l$$ (i.e. $$\ell$$) as variable name, it is too close to the digit 1 (one).
4. Don't use short integers (unless for a specific reason) or single precision variables.
5. Use $$i$$,$$j$$,$$k$$ as loop indices in a predictable way (e.g. for a (sparse) matrix: $$i$$ for rows, $$j$$ for columns, $$k$$ for things like nonzero elements).
6. The previous rule also applies to AMPL which uses local index names. E.g. after declaring
param f_max {j in FOOD} >= f_min[j];
I always use j for FOOD
7. Use short names for items (variables) that are often used and for locally declared indices. Use long names for items that are sparsely used. I call this Huffman-code [2] naming.
I am so used to this, that code that disobeys this in a flagrant way, just hurts my eyes. I find that, if I follow these simple rules, reading code is easier. It minimizes the surprise factor. Of course, writing code is for consumption by a compiler (or other tool), but more importantly: for consumption by a human reader.

So, that loop should look like:

const int n = 10;
for (int i = 0; i < n; ++i) {...}