Debunking popular myths - long variable names
Categories
When people first read Code Complete it changes their life. From the moment that you read that book your coding style changes and you are a different person. In the Code Complte book it is mentioned that you should use descriptive variable names - in fact I believe that it says that the variable names should be as long as they need to be to convey the purpose of the code item.
I really think that this has been abused by way too many people. Realistically, if your variable names are longer than about 15 characters then you must consider renaming them. Ideally, nearly all variable names would be a good deal shorter than that.
Long variable names indicate to me that the author of the code really didn't understand the purpose of the variable when they created it. Because of this, look for long variable names when you are trying to find:
- Buggy code
- Hard to maintain code
- Code that does the wrong thing altogether
The summary is that each piece of code should do one thing and that therefore the naming should reflect that. If you find yourself typing out a long name for a variable, class, or method then use that as an alarm bell to make you sit back and really think about what it is that you are trying to achieve.
#
Slightly longer descriptive versus short
posted by
Mitch Wheat
on
6/24/2006 11:58:59 AM
:
I agree that it can be and has been abused but would also make the following points:
I have seen the situation many times when a programmmer will construct a poor abbreviation just because some coding standard says that variable names should be at most N characters, and using the full descriptive name would have gone over by a few chars (i.e. 5 too many). So we end up with a 10 char cryptic (or ambiguous) name as oppose to an 18 character desriptive name. I'd definitefly prefer to see and read the latter.
In my view, an even bigger give away of suspect regions of code is when there is a mixture of very terse and very verbose variable naming.
I do agree that really long names are bad for the reasons you mentioned, but also because they make code harder to read, and therefore slower to understand, and therefore harder to maintain.
I guess in the end its about comman sense; I obviously try to keep variables as short as possible whilst maximising their meaning. My rule of thumb is slightly longer than yours: 32 character maximum length, although in practice it's rare that I'd ever name anything that long.
Regards,
Mitch
#
Variable names length
posted by
Joern Schou-Rode
on
6/25/2006 2:12:20 AM
:
I agree with Mitch on this one: Try to avoid long names, but do not make up obscure abbreviations in the quest to shorten your code! If you have a class or a method where the variable names are starting to get really long (20+ chars), have a break and consider if you have better to divide the class or method into several smaller classes/methods.
This post made me think of Roedy Green's website about how to best write unmaintainable code. He has some advices on naming as well:
http://mindprod.com/jgloss/unmainnaming.html
#
Depends on scope as well
posted by
Francois
on
6/27/2006 4:26:11 PM
:
I tend to see that there is a relation between the scope of my variable and the length of its name.
I do not put to much effort in variables used in small loops. I am more careful with class attributes for exemple.