Naming Conventions - The good, the bad, and the ugly

Published July 06, 2007
Advertisement
Naming conventions, a crucial part of your code. At their core, naming conventions are there to help in code readability, and depending on what convention you use it may help identify specific variable properties.

So what are the right naming conventions? How do you make sure your code is keeping up with today's standards? If you'd show off your code to your coding buddies, would they laugh?

Believe it or not, naming conventions effect whether someone determines that your code is messy or not. Other factors also determine this, but naming conventions play a large role.

What are the current most popular naming conventions? There are a few popular ones but people tend to incorporate them in their own flavors to suite personal taste.

Probably one of the most well-known naming conventions is the Hungarian Notation, originally created by some...well Hungarian guy who worked at Microsoft, Charles Simonyi. Hungarian notation may have been ideal in those days, but it is now(in most cases) unneeded.

One of the key features in HN was the ability to easily point out a variables type in a specific part of code without having to scroll back up to where it was defined. This was generally done by attaching lower case letters to the beginning of the variables name. For example if we had a variable dogname that was a string it would be renamed to something like strDogName. Simple, eh?

So why is Hungarian Notation bad? Well, one reason that it is now heavily avoided is because it's more then often unnecessary. Most IDE's like Microsoft's Visual studio have implemented features to easily identify a variables specifics without having to scroll back up to the definition. Microsoft's specific implementation is known as Intelli-sense. Because of these new features in today's more modern IDE's, Hungarian Notation is now deemed seemingly next to useless and generally adds to the noise.

The two other more ideal notations are Pascal, and Camel notation. In Pascal notation each word in the variable would start with a capital letter. Our variable dogname would be renamed to DogName.

Camel notation is like Pascal notation, except the first letter of the variable name is also lower case, so dogname would instead be dogName.

The Microsoft industry standard uses a combination of both the Pascal and Camel notation. Pascal notation is used for Class Members, where as Camel notation is used for method arguments and variables defined within a method.

The Microsoft naming convention. As much as we dread Microsoft, at least I can sleep knowing they did something right. On a side note, if you look at any of the API's Microsoft's made, like DirectX for example, they don't necessarily follow their own conventions.

Comments? What conventions do you use? Do you still use Hungarian? Why?
Previous Entry Dispwned
Next Entry Stuff to do..
0 likes 6 comments

Comments

Ravuya
I think we should've paid the Russians to leave Simonyi in space until he apologizes for the war crime that is Hungarian notation.

Personally, I use the .NET naming standard: use full words for things (no acronyms, short forms or cleverness), be consistent, use camelcase, etc.
July 06, 2007 10:17 AM
johnhattan
Descriptive names for everything except for trivial loop-counters. No prefixes. Variable names started with lowercase mainly because that's the standard for ActionScript. No underscores.
July 06, 2007 11:19 AM
bladerunner627
Quote:Original post by Ravuya
I think we should've paid the Russians to leave Simonyi in space until he apologizes for the war crime that is Hungarian notation.

Personally, I use the .NET naming standard: use full words for things (no acronyms, short forms or cleverness), be consistent, use camelcase, etc.


FxCop checks to see if you're following the .NET naming standard, and it will bark if you start a method's argument with a capital letter, same applies to any variables defined within the method's scope.

FxCop doesn't care if your class members are Pascal case, Camel case, or really any case for that matter, as long as they're not Hungarian it doesn't seem to mind.
July 06, 2007 12:58 PM
Driv3MeFar
Quote:Original post by Ravuya
I think we should've paid the Russians to leave Simonyi in space until he apologizes for the war crime that is Hungarian notation.


Hey, Apps Hungarian is actualy useful. It's Sys Hungarian that got it all backwards.

See: Joel on Hungarian Notation.

On topic: What I use at work is "whatever the hell everything else is in the area I'm working on". Sometimes it's Pascal style, sometimes it's Camel, sometimes it's Hungarian.
July 06, 2007 02:05 PM
Ravuya
Incidentally, yes, we use FxCop at work with the dictionary and anal retentiveness (retention?) cranked up to 11. It's a wondrous tool; makes me wish PC-Lint still existed so my home C++ code was as good as my work C# code.

Driv3MeFar: Apps Hungarian looks a whole lot better than the crackheaded version that's used inside the Win32 API. Guess we'd better leave Petzold in space instead.

Incidentally, the Apps Hungarian style is a refactoring pattern; that of the descriptive variable, which I use obsessively at the cost of performance (although I prefer to write something like unsafeName and safeName rather than usName and sName because us and s aren't words).
July 06, 2007 04:21 PM
Emmanuel Deloget
i don_t use any uppercase letter when i_m coding; and i like these lowercase and underscores; this way i stick to the c_plus_plus way of naming things, and this is something i find clean and easy to read;

camel_case and pascal_case are now a thing o the past in my code; to be honest, now that I_m used to this, i find camel_case and pascal_case disturbing.
July 07, 2007 04:35 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement

Latest Entries

Boo!

1539 views

Group blog

1044 views

Easy Usage

1063 views

Angel Script

924 views

My game idea

941 views

Lightmaps

912 views

Yay BSPs!

913 views
Advertisement