If I still use Hungarian Notation, am I a programming pariah? A Hungarian heretic? Part of the Axis of Programming Evil?
Here is why I use Hungarian Notation (HN):
- I can quickly determine the type of a variable by glancing at it (I don't have to hover over the variable with the mouse or anything)
- I can quickly determine the scope of a variable by glancing at it (the _ for class level, etc).
- Our existing code base uses it, and I'm anal about consistency and code uniformity
- I've been doing it for so long I seem to “think“ in HN; it comes naturally to me
I know HN is out of fashion and critics say it obscures the meaning of the code. I've always been a contrarian so I don't care if “all the other cool programmers“ use a style that I don't; and I don't feel it obscures my code. Given the following code that doesn't use HN:
DataObject.Save( name, birthday ) ;
It may be close to real English, but my analytical side wants more structure. I wonder: is “name“ a string? a structure encapsulating both first and last names as strings? a more complex object with first, middle, and last name strings . . . along with culture info etc? As for “birthday” the possibilities are more numerous: a DateTime? a simple Date? a verbose string for a date (like Tuesday, November 30th 2004); it could even be a boolean indicating if the birthday is today or not. You get the idea.
So, revisiting this with Hungarian Notation we could have:
DataObject.Save( strName, dtmBirthday ) ; //string and datetime
or
DataObject.Save( strName, dteBirthday ) ; //string and date (not using time component)
or
DataObject.Save( strName, blnBirthday ) ; //string and boolean
And so on . . .
Now, I don't discredit other coding methods -- I'm just explaining my position on Hungarian Notation. Use what you're most productive in (even Klingon Notation if you want -- which reminds me of this great Klingon post from a while back). I think the key element to a coding style is consistency. When maintaining code written by others, I can get up to speed quickly when there was some standard employed. If you don't do it for yourself, then think of the children. Those who come after you will thank you for it.
I just finished some enhancements to an application written by another company; the app was well designed. Yes, I avoid jumping on “the previous people who worked on this sucked!“ bandwagon . . . unless they really sucked, that is. It was smooth sailing except for their failure to name user interface controls in any style -- they just left the designer defaults so we had a huge array of Textboxes named TextBox1(n) or TextBox1, TextBox2 and so on for 30 or more controls (it was a good old tabbed user interface, so lots of room for controls). This was pervasive throughout the whole 25+ form application. And no, I don't think leaving controls named to their defaults to be a “style.“ That's just lazy. Give me a “txtTitle“ and “txtSSN“ any day!