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:
- 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; ... ). - i, j, k and m, n are always integer variables.
- Never use l (i.e. ℓ) as variable name, it is too close to the digit 1 (one).
- Don't use short integers (unless for a specific reason) or single precision variables.
- 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).
- 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. - 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.
So, that loop should look like:
const int n = 10;
for (int i = 0; i < n; ++i) {...}
No comments:
Post a Comment