THE PASZ.COM BLOG

Tuesday, July 26, 2005

Coding Style: Abbrvtns

One way to make your code really confusing is to use abbreviations. I will go so far as to say that you should almost never use them.

First, they make your code more difficult to read.
ddClr - does it stand for dropDownColor or dataDisplayClear or dungeonsAndDragonsCleric? Who knows?

Second, if you're consistent about always using complete words, it will be easier for you (and others) to use your APIs. You shouldn't have to crack open the documentation every time I want to instantiate a new Object. You should be able to guess the variable name without looking anything up. So if I want to instantiate a new TextFieldStyle Object, instead of guessing TFStyle or TxtStyle I just type the full words, and chances are I'll be right.

In the old days, when compilers weren't so good at code optimization, every character counted. So it made sense to keep variable names short. Now it doesn't matter. Just about any compiler will automatically compress your variable names under the covers. Look at the APIs for any modern language (Java, ActionScript, .Net). You'll hardly find any abbreviations. Now look at a typical C++ library for comparison. What a mess! Here are some of my favorites. Can you guess what they do? (Highlight below to see the answers.)
tmpnam - generate a unique temporary filename
swab - swap bytes
labs
- return the absolute value of a long integer

Maybe you're lazy, and you don't want to have to type so much. That is a good reason to keep names short. But remember, if your code is confusing, you'll waste more time in the long run. Just like writing in any other language, when you write code it is important to be clear and consistent. My 6th grade English teacher wouldn't have put up with an essay full of abbreviations, and neither should you!

1 Comments:

Post a Comment

<< Home