Should You Be Using a Naming Convention?

Even nonconformists will agree that when it comes to namingobjects and variables convention is a good thing.

Paul Litwin

October 30, 2009

4 Min Read
ITPro Today logo in a gray background | ITPro Today

DevNote

 

By PaulLitwin

 

Should You Be Using a Naming Convention?

 

How do you choose the names for your variables andobjects? Do you use some sort of naming scheme or do you just name thingswhatever seems right at the moment? There are a variety of opinions on themerits of the use of various naming standards or the lack thereof. A popularnaming convention originally created by Microsoft's Charles Simonyi andpopularized by Microsoft C programmers is referred to as Hungarian. It's calledHungarian for two reasons: Charles was of Hungarian ancestry and the fact thatat first glance it looked like a far-away language that no one couldunderstand.

 

The Hungarian convention is actually quite simple indesign. You prefix a descriptive variable name with an abbreviation, referredto as a tag, which tells you something about the type of data contained in thevariable. Of course, it helps if developers tend to agree on the same set oftags. In the past, different groups have settled on different sets of tags. I usea set of tags based on the conventions first proposed by Greg Reddick(originally along with Stan Leszynski) for Microsoft Access and Visual Basicdevelopers (You'll find the latest version of Greg Reddick's RVBA conventionsat http://www.xoc.net/standards/rvbanc.asp).Most, but not all, of the tags are three letters. For example, for stringvariables, I use "str". So a variable holding a person's last name might benamed "strLastName". Don't forget that the descriptive name is as important asthe tag you use. A variable name that uses the right tag but tells you nothingabout what it represents is pretty much useless.

 

Greg hasn't updated the conventions in a few years andthey don't specifically address ASP.NET as of yet. However, I just spoke withGreg who is working on a .NET version of the conventions (for both VB and C#),which should appear on the xoc.net site within the next few months.

 

I use Hungarian tags for naming value and object variables,as well as controls. Of course, since the RVBA conventions don't specificallyaddress ASP.NET and the .NET Framework classes, I've had to come up with someof my own tags. For example, I use txt for textbox, drp for dropdown control,and so forth. Thus, I would name a textbox containing a person's last name"txtLastName". Similarly, an instance of a String Builder class that I used tobuild an output message might be named "sbMessage". This String Builder examplerepresents a case where a two-letter tag works better than the standardthree-letter tag.

 

Choosing a tag can be tricky at times. The basic rules arethat tags should be unique and easily differentiated from other similarly namedtags. Often the first few letters from the type works well. Sometimes, however,you'll want to throw out insignificant vowels (e.g., dbl for double) or use thefirst letter of each word of a multi-word type (e.g., rev for the RegularExpressionValidatorcontrol). When it comes down to it, personal preference may be the decidingfactor. For example, although we agree on most of the tags we use, Ken Getzlikes to use cnn for an ADO.NET Connection, whereas I prefer cnx. And as youcan see from his article in this issue, Wayne S. Freeze uses conn.

 

There is, of course, a whole camp out there who thinkstag-based naming conventions are a bad idea. Many believe that tags aredifficult to read and intimidate beginning developers. Although tags appear alittle bizarre at first glance, I'm guessing it takes only a few days to get usedto them. And the use of a tag-based naming convention reduces type-related bugsand makes your code easier to maintain.

 

Just surveying the six articles with code in this issue Isee about a 50-50 split in those authors who use a tag-based convention andthose who do not. The choice is up to you, but even if you don't like usingtags, you should standardize the naming of variables, objects, and controls inyour ASP.NET applications. Anything less is likely to be an exercise infrustration if you do any significant amount of development, especially if youwork in a team-based environment.

 

Paul Litwin is editor and technical director of asp.netPRO magazine. Readers may contact himat [email protected].

 

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like