I went to a
NetDUG meeting last night on ASP.NET Best Practices, given by Carl Prothman. See the write up
here.
Overall the presentation was fine, he have a lot of good information, tips, techniques, and so on. The one part that I took issue with was where he talked about
Hungarian notation. He stated that for C# programmers, Hungarian notation should not be used. His premise was:
1. C# is strongly typed already
2. Visual Studio environment gives you all of the information anyway. All you have to do is hover over the variable.
3. There was also the reason that if the variable type has to change, you then have to change the variable prefix all over the place.
Here are my argument against his.
- Delphi, C, C++, VB, Java, and many other languages are also strongly typed, but it is still considered best practice for those languages. I really like C#, but I dont believe it has done anything to make Hungarian Notation obsolete.
- Great, so now if you print out the code, how is Visual Studio going to help you. Also, why should I have to move my mouse just to figure out what type a variable is, what it's scope is when I can just look at it. If I have to take my hands off of the keyboard, you are wasting my time. Also: I'm sorry, but you should not, under any circumstances, assume that everyone reading your code will be using Visual Studio, or any other environment for that matter. With the onset of Mono, SharpDevelop, and the new Delphi environment (which supports C#) that is dangerous argument. Programmers are fickle and prone to change quickly, tomorrows Visual Studio.NET might be Emacs (or VI -- cough).
Motive 2: If I'm having trouble with a procedure I've written, I'll bring in another developer to look at that procedure. I dont want him to have to inspect the entire class just to figure out where the variables are declared. Now you are wasting the time of two developers.
- Haven't any of you people heard of "Search and Replace"? It has been a standard feature for years and has become very useful to the modern programmer. Changing a variables type is not a small matter anyway. Any time you change a type you should go through every place where that variable is used and make sure that it is still going to work. Think of it this way, one of the tips that Carl gave was that when you check in your code to your source code control (Source Safe, Vault, CVS, etc), you should inspect EVERY line you changed. When you change a variable type, you have affectively changed every line that variable is used on. What better way to make sure you look over every line than to change the variable name.
Finally I'll give a few environment of my own, not related to his.
I work in a large enviroment with many developers. Typically, one developer will start a project, work on it for a while, then hand it off to another developer, who has to make more changes. That second developer will have code from many other developers in his hands at once and has to be able to read it all.
Now let's be frank. C# allows developers to write some pretty sloppy code. Variables can be declared anywhere, in any form. They might be prefixed with
private or
public, or they might not. You can declare a exception class as
IgnoreMe instead of
IgnoreMeException. Curly brackets can go anywhere. C# is not a structured language. It is up to the programmers to add structure, and frankly, not using Hungarian Notation is just adding one more inconsistency variable into the mix.
To sum up: Hungarian Notation makes code easier to read and understand for other developers, removes reliance on other development environments, and up to this point, every argument I've heard of for getting rid of it has been insubstantial and petty.
If anyone cares to prove me wrong, please do.