Naming Conventions

General Development Concepts

Best practices for naming identifiers in your code.

Date : 2006-02-07
Naming Conventions
d. "A set of rules for choosing identifiers"

There have been a number of naming conventions that have been pushed on programmers over the years with varying degrees of success and for various reasons. Two of the most commonly used naming conventions are "Hungarian Notation" and "CamelCase" (aka. BiCapitalization, InterCaps, MixedCase)

Hungarian Notation was invented by Charles Simonyi from Microsoft. Whether it was called "Hungarian Notation" because Simonyi was from Hungary or because it looked more like a foreign language than English may be lost to us at this point.

The purpose of Hungarian Notation was to embed the variable "type" into the variable name to make it easier to remember so we would see variables named:

* sName, or strName for a string that held a name.
* iMax, or intMax, or even i_max for an integer that held some Maximum value.

A simplified rule for Hungarian Notation could be stated like this: Identifier Name = type + purpose.

One of the arguments against using Hungarian Notation is that true Object Oriented programming languages no longer have a strong type structure like we used to. Each identifier is an object and can contain data of various types. While this is true not having strict type casting may be an even stronger reason to keep track of a variable's type ourselves.

For a more detailed discussion of "Hungarian Notation" including how it can be applied to function and procedure names you can read an updated version of the original article by Charles Simonyi on Microsoft's website Hungarian Notation

I imagine every programmer has seen this naming convention used and may even have adopted some of it's features as there own.

The other Naming Convention that was mentioned is "CamelCase". There are several variations on Camel Case which basically determine whether or not you are going to capitalize the very first character. Camel Case is more for the purpose of making long and descriptive identifiers easier to read. Examples of CamelCase would be:

* userFirstName, which is obviously a user's first name.
* LoopMaximum, for the ceiling on a loop.

Most users of Camel Case will find themselves shortening these names before long.

More can be found about Camel Case on Camel Case

Both of these Naming Conventions show the purpose behind having a naming convention. It is to make it easier for us to debug our own and others work. Being able to determine what a variable is for simply by looking at it is not to be taken for granted. If anyone has ever seen code that has been obfuscated (identifiers and formatting confused on purpose) it is clear why we have naming conventions.

So whether you decide to use one of the previously specified naming conventions or come up with your own it is clearly a good idea to have a naming convention of some kind. Most programmers after spending a few years coding will settle into one of their own and stick with it.

Comments :

No comments yet
  • Search For Articles