Should You Be Using a Naming Convention?



By Paul Litwin


Should You Be Using a Naming Convention?


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


The Hungarian convention is actually quite simple in design. You prefix a descriptive variable name with an abbreviation, referred to as a tag, which tells you something about the type of data contained in the variable. Of course, it helps if developers tend to agree on the same set of tags. In the past, different groups have settled on different sets of tags. I use a set of tags based on the conventions first proposed by Greg Reddick (originally along with Stan Leszynski) for Microsoft Access and Visual Basic developers (You'll find the latest version of Greg Reddick's RVBA conventions at Most, but not all, of the tags are three letters. For example, for string variables, I use "str". So a variable holding a person's last name might be named "strLastName". Don't forget that the descriptive name is as important as the tag you use. A variable name that uses the right tag but tells you nothing about what it represents is pretty much useless.


Greg hasn't updated the conventions in a few years and they don't specifically address ASP.NET as of yet. However, I just spoke with Greg who is working on a .NET version of the conventions (for both VB and C#), which should appear on the 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 specifically address ASP.NET and the .NET Framework classes, I've had to come up with some of 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 to build an output message might be named "sbMessage". This String Builder example represents a case where a two-letter tag works better than the standard three-letter tag.


Choosing a tag can be tricky at times. The basic rules are that tags should be unique and easily differentiated from other similarly named tags. 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 the first letter of each word of a multi-word type (e.g., rev for the RegularExpressionValidator control). When it comes down to it, personal preference may be the deciding factor. For example, although we agree on most of the tags we use, Ken Getz likes to use cnn for an ADO.NET Connection, whereas I prefer cnx. And as you can see from his article in this issue, Wayne S. Freeze uses conn.


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


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


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




Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.