Common Genius

The online technical home of David Nelson
Welcome to Common Genius Sign in | Join | Help
in Search

Variable Irony

A commentary on technical issues ranging far and wide.

Hungarian Notation - The Good, the Bad, and the Ugly

While blog-surfing recently I ended up at an article about the ups and downs of Hungarian notation: "Hungarian Notation - The Good, The Bad, and The Ugly." The Good makes some good points, although I personally I don't believe that they are enough to overcome its shortcomings. The Bad is poorly written, in my opinion, and does not add anything substantial to the argument. The Ugly, however, is very well written and makes several good points. It expresses my primary argument against Hungarian notation quite clearly:

"Hungarian notation encodes type information into variable names. This is very useful in languages that don't keep track of types information for you. But in C++ or Eiffel it is completely redundant. Thus, the notation simply adds to obscurity."

It also expresses a point which, although I was aware of it implicitly, I had not been able to express previously:

"Hungarian notation is, when all is said and done, a commenting technique. And the one great law of comments is that they lie."

This is precisely true. Hungarian notation, at least in the form that most of us learned in school, simply says "this variable is this type." Its like a comment. But comments quickly become outdated as programs change, and often in the end they are more misleading than helpful.

I do still use an underscore before class-level variables, but this is mostly to distinguish them from the properties which they typically represent (In C# I could rely on casing differences to make the distinction, but going down that road is just asking for death-by-typo).

Published Thursday, January 11, 2007 1:06 PM by dnelson

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Luke said:

In fact this is a misinterpretation of Simonyi's original intent for hungarian notation.  Check out this excellent joelonsoftware article on the subject: http://www.joelonsoftware.com/articles/Wrong.html

It is not intended to be used the way microsoft does, where every variable's type is encoded into its name.  Instead its intended to provide more information than what the variable's type tells you.

February 6, 2007 1:55 PM
 

Ashish Basran said:

Well said.

April 12, 2007 6:33 PM
 

Reinier Zwitserloot said:

I used to have my own little variant of hungarian for java programming; prefix any local variable with an l, parameters with a p, fields with a f, static fields with an s, and where relevant, GUI stuff with a w. You'd have:

public void setSomething(int pSomething) {

fSomething = pSomething;

}

instead of public void setSomething(int something) {

this.something = something;

}

which can quickly lead to a bug if you forget the 'this'.

I stopped doing this, partly because in the end hungarian just blows, but mostly because of the redundancy argument. These days I can configure eclipse to make static stuff italics, something = something generates a 'this statement does nothing, so I doubt you meant it' warning, and if I really want to see the difference between locals, parameters and fields that badly, the editor can do this work for me.

You do still see variants of hungarian in other places. In python, for example: "_" in front of any member is 'hungarian notation' for what is better known as 'private'. As a rule, if you're going to invent some sort of standardized notation for something, might as well go all the way and make it official. That way your toolkit can make assumptions with more gusto.

I 'get' where hungarian came from though. It's one of the grandfathers of the intelligent editor. The fact that whomever came up with hungarian didn't just look at e.g. squeak and realize that he should just write an editor that clarifies the issue is a bit of a shame, but you can't win them all.

May 24, 2007 2:37 PM
 

dnelson said:

Somehow I didn't know until today that these comments from months ago had been posted. Sorry about that.

@Luke: Yes I am aware that the form of Hungarian notation most commonly preached, especially by Microsoft (until they abandoned it in the .NET framework) does not follow the original intent, which can actually be quite useful (that's what I was alluding to by "at least in the form that most of us learned in school").

@Reinier: I have run into the "this" bug in both C++ and C#. That's one of the things that caused me to go back to using underscores for private variables. I still have yet to find a notation system that I really like; it evolves with every project I work on as I try to find something that feels natural and doesn't get in the way.

November 1, 2007 6:11 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems