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:

userNameField

or

userNameBox

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:

termsAndServicesLink

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:

termsAndServicesLinkButton

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.


4 thoughts on “How I use Hungarian notation?

  • Bertkid says:

    I use Hungarian notation on all my controls as well, but no where else.

  • AndrewSeven says:

    I try to put some semantic information into the name, so if I need a label that will contain “username :” I will call it UsernameLabel.

    The “Label” suffix does not imply the type, the control could be anything (usualy its a Literal or a Label)

    Side note: as far as I know, the simple prefixes like “str” and “i” are not really hungarian notation.

  • The Janitor says:

    We don’t use Hungarian notation in .NET code, as proposed by Microsoft’s guidelines for class library developers.

    Concerning control fields we see it as follows: A TextBox providing the first name (e.g., of a customer) becomes “firstNameTextBox”.

    As I’ve seen, some others utilize Hungarian solely for those control fields, but not for other fields or variables, which might be OK as well.

Leave a Reply

Your email address will not be published. Required fields are marked *