Good variable names

In research and programming, the word ‘variable’ occurs but with completely different meanings. In research a variable is a thing that is being measured. It’s data will fill a column in a spreadsheet or a field in a database table. In programming, a variable is a temporary bit of memory that stores one thing, a number, a date, a bit of text and so on.

In both spheres, variables need a name and contrary to what one might think, this is not a trivial thing. It is important to have a good name because nobody actually reads the documentation well, and often there is no good documentation. The only documentation is the name and perhaps some settings or formatting. So, take it seriously.

I can come up with a minimal set of criteria for the naming of a variable.

  1. The variable name has to make sense in the context where it is used
    Duh, … but I heard of variables called ‘values’ or ‘code’.
  2. It has to be specific.
    The name as to be exact enough for the context that it is used. For example, if there are no other ‘dates’ in the context of use, then the name ‘date’ would suffice. If there are other dates, then it obviously would not. Is it the date of data entry or the date of a visit?
  3. The variable has to be descriptive rather than indicative
    For example naming a variable ‘essential’ does not say a lot. Describe what it is that makes it essential.
  4. It can be as long as is needed
    We do not live in the 1980s anymore, where eight characters was considered a lot. So stop using those TLAs where you can, because you will forget what they mean, you will not document it and nobody will read that manual. And if your particular software does put severe limitations on the length of a variable name, then it might be a bit outdated.

I think the whole list can be summarized as : does the name explain to a relative outsider, what the variable is about and how it should be used? That relative outsider is someone who has similar knowledge but doesn’t work on the very same thing as you. It can be a colleague at work, but also yourself ten years later.

Do you know of any other criteria?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Website Powered by WordPress.com.

Up ↑

%d bloggers like this: