Use descriptive variable and type names

Development Tips

Use descriptive names for everything you name. There is a lot more involved in naming variables than picking a letter.


Date : 2009-04-24
There is a lot more to selecting a name for a variable than just picking a letter. In fact, there are very few times when a 1 letter variable name would be acceptable. Usually you will want to select a human readable variable name that is descriptive and accurate to describe what you're talking about.

This applies to more than just variable names, it really applies to anything that you name in your code.

The examples below are shown in "camelCase" but obviously the same rules would apply no matter what naming convention you're using.

Some rules of thumb:
#1 Simple variables that hold a value should be named after the value they hold: (ie. a variable that holds the name of a song should be called songName). In most cases then a variable will be a noun.

#2 Functions are going to do something so with your variables (nouns) making functions the programming equivalent of verbs. For example if a function were to return the color of an item, depending on the actual process involved in getting that color, a possible name could be getColor(item), "get" being the verb. However, if the function is going to return the color of the item a function call isBlue(item) is just going to confuse other programmers, and you when you haven't looked at the code in a while.

#3 Functions that return a boolean value should be named in such a way to make that obvious. Examples of this are: isEmpty, isNull. When creating your own boolean functions stick to these patterns.

#4 Avoid misspellings in naming. I would include abbreviations in this unless the abbreviation is obvious within context. I worked on a code-base that had the word "anonymous" consistently misspelled "anonimous", I'm already not a very good speller so every time I went to track down something related to that code it took an extra few minutes to remember how it was misspelled. I did eventually change them all but it was not an easy task. I know many code editors don't have spell-check built in, but do a quick google search on the word to see if it's spelled right or not. I think it's pretty standard for programmers to be bad spellers, I'm not sure when that stereotype got started but I think it's at least more than 50%, which is all it takes to get a stereotype started. Don't embrace this, fight against it, get whatever tools you need to spell your variables correctly.

#5 Be consistent. The best way to handle naming is to work to a coding standard and be consistent. If you always name variables, functions, classes, even files by the same set of rules you will always be able to read your code easily.

When selecting names for these items you want to think, not only of how you will understand your code, but also how will the next person who has to look at it understand it. The idea of programming is not to make your code as difficult to understand as possible in the hopes that this will make you look more intelligent. Instead the sign of someone great in their field is that they make it look easy. Do the same thing with your code. Make it easy to understand by selecting descriptive, easy to comprehend names.

Comments :

No comments yet
  • Search For Articles