How to choose a good variable name

By 4th April 2015Programming

Before I get into this weeks topic of choosing a good variable name I have two notices below, then we will get right in.

I have decided to change the scope of my blog from IT Security to IT Extraordinaire as I want to include a wider variety of topics which interest me, such as computer programming and Arduio development.

I had some feedback on the post I did about trying to gain access to a computer. The point of the article was to show that there are ways to hack into computers, so you should ensure that your Windows, Linux or Mac computer have all the latest patches and are as secure as possible.

Ok lets get in…

Today I am going to discuss what make a good variable name. Variables are used in programming languages to store a values. Once a value is stored in a variable, the variable may be manipulated so that the variable contains a new value based on the manipulations performed.

I will do a follow up article to discuss what a variable is.

When naming a variable while programming we often just want to choose a short quick variable name something like ‘x’. This is a classic example of a variable name which does not describe the data which it stores. The problem with this is that a year later when you or someone else needs to perform an update or maintenance to the code you have no idea what ‘x’ is, and then need to spend extra time trying to work out what the value ‘x’ stores.

In my opinion is it better to choose a name which is a little more descriptive, like unitCost,or you can even be more specific, unitPeakCost. In fact I would recommend to be as specific as possible.

To add, sometimes very long and descriptive variable names are useful, and in other cases they are not so useful, use some discretion and read my little story below.

Descriptive but short names. I once had a colleague who was battling to troubleshoot his program, and I got involved to help him out, and after a good while of time I decided to copy and paste his variable name in all the places where he had used it, just to check that the variable name was in fact spelt correctly. And yes, I found that because the variable name that he had used was so long that he had actually made a spelling mistake in other critical places where he was using the variable name. This was in the PHP programming language using JEdit. PHP allows for variables to not be declared, so no error is thrown for an incorrectly spelt variable name. Keep your variable names short enough and descriptive enough.

Use camel case. Use camel case to separate the words in your variable names. Most programming languages recommend that you start off your variable name with lower case, and then each subsequent word starts with a capital letter. Such as ‘unitCost’, and where there are more than one word, ‘unitPeakCost’. When there is only one word do not make any letter uppercase an example would be ‘cost’.

Use full words. My next suggestion is to use full words in the variable name, by doing this, you don’t leave it up to the reads imagination of what your variable name described.  As an example avoid using abbreviations where it makes the description vague such as ‘unCost’. A year later you may not remember why you chose that name.

Well that’s all from me now, if you found this article helpful, please leave a comment. Or just leave a comment anyway.

Enjoy your Easter holiday.


Author peter

More posts by peter

Join the discussion 6 Comments

  • Kris K. says:

    One of the best advices i got about naming is this one ( courtesy of Uncle Bob ):
    “The larger the scope of the variable, the shorter the name”
    It basically mean you should name your local variables with long descriptive names, but prefer to use single words for globals, keeping them as descriptive as possible. This works great because variables with large scope tend to have more generic meaning, and the ones with small scope are usually storing some details that need more description to be comperhensive.
    I like the way your blog is heading 🙂

    • James Murphy says:

      I was just about to suggest this. If you use proper OOP with short classes that have a single responsibility your variable names naturally start to make sense.

    • Thanks for the comment. I agree, although I think one should avoid single letter variables unless it’s used in a control loop. Also I think properly spelt words are also better.

      • Kris K. says:

        True, but still using a one letter variable as index for some 1-line for loop is ok. You gain nothing by changing “i” to “indexOfStuff” in such case. It might even make code more obscure.
        Although for a 3-line loop i would rather recommend avoiding 1 letter variables. 😉

  • Junald Bayog says:

    Very fundamental topic but most important practice in software development!

  • Kevin O'Shaughnessy says:

    I am currently reading Code Complete by Steve McConnell. There is a chapter in this book where he goes into great detail on naming variables well. I think it is important to name boolean variables in a self describing way. For example if you have a class called Lightswitch you can call the boolean isOn and when you write if (LightSwitch.isOn) it is a very English like way to read it which makes it easy for anyone to understand.