thinkingincode.ninja [Dark]

Appropriate use of single letter variable names

Suggested previous reading: What is "clean" code?

There are few things that instantly make code less readable than the use of single letter variable names in places where they really don't belong, or the use of the wrong single letter variable names. Here, I'll go over general appropriate use of such names.

Loop indices and counters

The variables i, j, and k, should only be used for counters or indices inside of loops. They should be used as needed in that order, if you have nested loops, always starting with i. Further, unless you have an extremely good reason not to, you should only use those variables for numeric loop indices (with the exception of positional coordinates, detailed farther down).

// Prints the first 10 multiples of num
int num = 7;
for(int i = 1; i <= 10; i++) {
    printf("%d times %d = %d\n", num, i, num * i);
}

Constant sizes

The only context in which n should be used is for a static count or size. You should never change the value inside of n (ie n should be treated as a constant). Generally, n is used to indicate how many times something will be done.

void print_message_repeatedly(string message, int n) {
    for(int i = 0; i < n; i++) {
        printf("%s\n", message);
    }
}

Measurements

It is acceptable to use the variables l, w, and h to refer to length, width, and height, respectively. Personally though, I still prefer to spell out those variable names, because it makes it clearer. However, it's usually clear from context (such as the name of the function that the variables are being used inside of, such as CalculateArea), so it's fine. Just don't use the variables for anything but length, width, and height, and it'll be clear.

Coordinates

When referencing positions/coordinates, x, y, and z are fine to use. I generally use posX, posY, and posZ, instead, with pos being short for "position", just to be more clear; also, if you are working with coordinates and positions, there's a good chance there will be more than one set of coordinates in use simultaneously. In this case, you should really name your variables with a prefix of what the coordinate is for: playerX, playerY; mouseX, mouseY; spawnpointX, spawnpointY, spawnpointZ, etc.

It's fine to use x, y, and z as loop index variables, only so long as you are using them for coordinates. For example:

// Prints a checkerboard pattern, where % is the modulus operator.
for(int y = 0; y < 10; y++) {
    for(int x = 0; x < 10; x++) {
        if((y + x) % 2 == 0) {
            printf("X");
        } else {
            printf(" ");
        }
    }
    printf("\n");
}
/*
Output is this:
X X X X X
 X X X X X
X X X X X
 X X X X X
X X X X X
 X X X X X
X X X X X
 X X X X X
X X X X X
 X X X X X
*/

Why it matters

There's only so much meaning that you can pack into a single-letter variable name. A variable named m could have anything in it, but a variable named message very likely has some form of a message in it. Another way of putting it is this: The more accurately the names of your functions and variables and classes match the contents, the easier it is to read. A variable named x will only be an accurate reflection of its contents if it stores an x-coordinate.

It also matters that general conventions are followed. It has long been established that i is a numeric loop index variable, so to use it for any other purpose makes things at least mildly confusing for whoever is reading your code. If these conventions don't seem like a big deal, then it shouldn't be a big deal to follow them.

Note: If you are implementing code that is based off of a formula or algorithm, or which is heavily math based (such as might be found in something involving calculus, cryptography, or physics), feel free to disregard the general guidelines in this post, and use single-letter variable names as appropriate if they can be clearly mapped to the formula. Try to be sensible and don't go overboard with every variable being one letter, but with math-heavy code, your audience will typically be other people who are well-versed in the relevant math, and so they will have preconceptions about what i will be used for (though n probably, but not always, holds the same meaning). For example, in public key cryptography, n refers to a modulus derived from multiplying two very large primes p and q together; personally I'd choose to use the variable names modulus_n, prime_p, and prime_q to reference these variables, so that it's explicitly clear what's what, but you'd probably be fine using the single-letter names too.