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.