Long variable names similar to English are easy to read (but not “crazy” long). If in doubt be verbose rather than use abbreviations.
Some real examples are listed in the table below:
Variable |
Better Variable |
Why |
---|---|---|
rc |
recordCount |
rc could stand for many things. |
lstItmCntr |
lastItemCounter |
The abbreviation saves only a few letters – the small extra cost to write 'lastItemCounter' adds much readability. |
totalOfAllInvoicesIncludingPaidAndUnpaid |
totalPaidUnpaid |
This variable is just too long for general use because it affects maintainability by causing too much code wrapping. totalPaidUnpaid would have been fine. |
canDeleteAllTheChildrenAndDogAsWell |
canDeepRecursiveDelete |
This one was just too long, and only made sense to the author. |
s |
session |
So many words start with 's' – it is difficult to work out what 's' might have been an abbreviation for. |
theFile |
inputFile |
Clearly shows the usage of the file. In this case it describes the file to read from. |
file |
currentProcessingFile |
Files can be used for many purposes. In this case the variable stores the current file being processed. |
idList |
videoIdList |
If it is a list of videos, why not call it that? |
found |
foundIndexFieldComment |
Developers tend to use the word 'found' regularly, leaving others to guess what has been found. |
flag |
hadLinkTableTag |
Another flag where the reader was left guessing what kind of flag it was. By avoiding an abbreviation like 'ltt' or 'flag' the coder improves maintainability. |
Tip: It can be a fine balance between helpful text and too much text. For example, if function or variable names do not fit inside an editor window it will hamper productivity. If up to four concise words can't describe what something is, perhaps you need to structure your code in a different way, or use a thesauris to find a shorter version of a word.