14 07 2008
How I use Hungarian notation?
Well, this post is not my attempt to start millionth flae war about variable naming conventions. It is my explanation how I use Hungarian notation and why. By the way I am not dedicated fan of any convention, so starting flaming here has no point. So let’s see why and how I use Hungarian notation.
I found Hungarian notation not very good convention when naming variables in my code. If my class library has all identificators as integer I don’t want to use namelike iId. Also strUserName is something I don’t like because it containt too much information – I know that username is string, so why should my code tell readers something they already know.
Years ago when I coded mostly on Visual Basic 5.0 and 6.0 the Hungarian notation was widely used in applications. Although there are other popular methods for naming variables I took Hungarian notation with me from my past because there is one very good way how to use it with other naming methods.
Usually we see variables like _invoices or m_invoices or invoices in code files. Some guys name also controls like this. I found this to be very bad idea because for controls I want to know exact type they have. If I see something like this in my code:
I want to ask what the hell is this field? Is it TextBox or Label or something else? Should I disable it for details view or not? If I draw box around Label it is also visually box as TextBox is.
The other example is related to links. Let’s see the following control name:
What is the type of this form field? Is it LinkButton or Hyperlink or is it HTML <a> tag with runat="server"? There is nothing that tells me what kind of control it is.
Okay, I can easily find out the type of control, I just have to move mouse on the name of it and then IDE shows me what it is. Of course, I can always use keyboard shortcuts too. Although it is another very good flaming topic (keyboard or mouse) I want to point out the other thing – reading the code is interrupted because, for awhile, I had to focus on something else.
One may argue that butting type to the end of name is solution to problem:
Okay, it gives me more information, that’s correct. The problem is – I read from left to right and that’s why this solution is not convenient for me. When I look for some control in longer method I cannot go through this code fast because I have to watch end of the names used in code. If I can check only the left side of names I go through code much faster. And whenever I see something like xxxSomeName in code I know for sure it is some control.
These were my two little points about Hungarian notation.