Hacker News new | ask | show | jobs
by haberman 3707 days ago
Think of short variable names as pronouns. Like saying "it", except you have maybe 1-4 distinct "it"s. Short variable names work well for things that have common and obvious meanings. "i" for an iterator or loop index (add "j" and "k" if you have nested loops). "fd" for a file descriptor. "n" for a count of something. "p" for a pointer. "ofs" for an offset. Other conventions can apply within a codebase.

Short variable names work less well when you have more than about 4 of them in the same function/block or there is no common convention suggesting what the variable might be from its name alone.

Short variable names are also not good for globals or struct members, because those names are used across many contexts. Combine this convention with a short local variable name and you end up getting things like:

    typedef struct {
      int retry_count;
    } connection_t;

    // If "connection_t" is used widely throughout your codebase,
    // you get used to seeing a variable "c" that is a connection.
    void func(connection_t *c) {
      fprintf(stderr, "Retry count: %d\n", c->retry_count);
    }
...which I think is pretty readable.
1 comments

The prevalent opinion* is that it doesn't play well with existing standards to use `_t` in your own code, although personally I find an extension which tells me that it's a type to be very helpful.

[0] http://stackoverflow.com/a/231807/3467349