Monday, December 7, 2009

Y us crptc var nm?

There was an interesting article in Omninerd about the need for coding standards and, although you've probably heard most of it before, I'd be interested in feedback about one particular point - cryptic variable names.

We all do code maintenance and (I'm sure) we all encounter code which seems to make no sense at all until we mentally remap the existing variable names into something more understandable.

So why do people call a variable 'dtFHire' when they could name it 'DateOfFirstHire' (or even 'dtOfFirstHire')? Is it an anachronistic habit from days when memory was scarce and variable names limited in length? Is it because they don't like having their code run wordwrapping when they write:
WeeksOfLeaveAccumulated = ((LastPayDate - DateOfFirstHire)/NumberOfDaysInAWeek) * DailyLeaveAllowance) + BonusWeeksAccumulated - NumberIFirstThoughtOf

What is the reason?
.

3 comments:

Greg Charland said...

Two reasons:
1. We "old school" developers remember a day when each character of a variable name took up a byte. Longer variable names = more bytes of code. When you've only got 16K to work in these things matter...obsolete but old habits die hard.

2. Typing Num1 takes a lot less time than intNumberIThoughtOfYesterday. If I'm working with a couple hundred variables it does save tons of time to use shorter names and comments. Not saying it's best practice by any stretch but developers are human too (mostly). :)

Dan Sickles said...

I agree that cryptic names stink however there is a point at which long names add noise rather than clarity. Descriptive names with sensible abbreviations are my approach.

Chapter 2 of Clean Code, "Meaninglful names", is 12 pages of pure gold.

Urs Meli said...

#1 reason I used to use short variables: No TypeAhead in the designer client < 8.5.1 !