Saturday, May 9, 2009

Hungarian Notation

Don't use Hungarian Notation in C# or Dot.Net
strName
The reason Hungarian Notation has been deprecated for .Net programming is because it is no longer needed.
 
Historically, the compiler did not perform the strict type checking that we now get with .NET and therefore it was a benefit to know what the Type of a given variable was just by looking at its name.
 
Now however, we really couldn’t care less what the Type is since, the compiler will highlight any mismatches. OO programming offers polymorphism and hence variables can be of many different Types at the same time.
 
Additionally, if all our variables were originally of Type “string” and hence called strName (under Hungarian Notation), what would happen if we decided to modify the Type of the variable to a new object called “Name” object, should we then go and rename all our variables “strName” to “nameName”. Of course not. We should just have used “name” in the first place and no changes would be necessary if we decided to modify its Type at some point in the future.

No comments: