Do You Speak Hungarian?

Intro

For anyone that doesn't know, Hungarian notation is the practice of adding a few characters to the beginning of a variable's name to denote the type of the variable. For example, a variable that stores the username as a string might be "strUsername".

I find it odd that something so trivial can cause such a heated debate. The question of whether or not to use Hungarian Notation has come up on every development project I have been on, and it was always hotly contested.

Cons

The first time that I was told that I had to use Hungarian Notation, I was less than thrilled. I started out programming in VB, where the type of a variable was fluid, and sometimes changed over its lifetime. This directly conflicted with the idea of noting the variable type in its name. In fact, it was ludicrous. In the .NET world of strong-typed variables, however, that argument falls apart rapidly.

The next argument is that it is a waste of time. "Why do I have to type 3 extra characters every time I type my variable name?" 3 characters is not much, but multiply that by each time a variable is declared or used, and it becomes significant quickly. This is probably the best argument against using Hungarian Notation. The best counter argument that I can make is that Visual Studio’s intellisense feature saves so many keystrokes that it (at least partially) negates the cost associated with Hungarian Notation.

In a last-ditch effort, the opposer will play the maintenance card. "What If I declare a variable as type X, and later need to change it to type Y? I will then have to change the name throughout the application, which will take considerable time." This may have been an issue ten years ago, or for those of you still using notepad to do your development, but every modern IDE I know of has a "rename" feature (or at least a global replace feature).

Pros

Have you ever found yourself in the code-behind trying desperately to remember what you named a particular control on the UI. You eventually have to give up and switch back to the UI for a moment to find out. I have even seen this happen in canned professional demo presentations. If you are using Hungarian Notation, all you have to do is type "txt" (assuming you are looking for a textbox) and intellisense will provide you a list of all of the textboxes on the form. Pick the one you want and keep going, all without leaving the code-behind. This, in itself, is not a reason to switch to Hungarian Notation, but it is a nice time-saving feature.

Another basic benefit of Hungarian Notation is that there is no question what types of variables you are working with. As such, the programmer is much less-likely to accidentally create type-casting errors. Consider:
name = id;
Vs.
strName = intId;
Simply the act of typing the second line should throw a flag for any seasoned programmer.

The real reason for using Hungarian Notation provides little bennefit to the indivual programmer. Hungarian Notation (along with a set of fully documented coding standards) promotes unity among a group of programmers, and a consistency in coding style. Code written by John Smith looks similar to that written by Jane Doe. Code written in 2008, looks the same as code written in 2009. So now you are thinking: "Why do I care if all of the code looks the same?"


I feel like I could go on forever, but you get the idea.

Conclusion

While it may be painful to implement for the first time, once you get past the "Why do I have to do this?" mindset, the benefits are well worth the few seconds it takes to implement. However, these benefits are only seen if Hungarian Notation is adopted by the developement team as a whole. If only one developer in a team uses Hungarian Notation, they are possibly doing more harm than good. The true key is to adopt Code Standards as a team, which, in my humble opinion, should include Hungarian Notation.