Wednesday, June 8, 2011

Hungarian notation

Hungarian notation

Is the devil. Or not. Or maybe it's simply a neutral element like a ham and cheese sandwich. Meh, let's just write some philosophical and overly cynical prose about it. Oh, and those of you who aren't familiar with programming ideas or don't like reading about them, may get in line at the volcano entrance to my lair.

Hungarian notation originated way back in the day. It came from people who didn't like proper variable naming, and had no concept of which information was useful or merely a waste of space.

Hungarian notation is basically one of two systems where you prefix your variable names with extra info about them. So if your variable which is called "numCustomerSunglasses" was an integer, then by one system you would change it to "iNumCustomerSunglasses." By another system, you might classify it like so if it was used for stats analysis: "statNumCustomerSunglasses."

I am/was EXTREMELY opposed to Hungarian notation, and I noticed something in my code today:


                debug = true;

This is a form of Hungarian notation, and a perfect example of why it's so foolish. I scanned my code and found that some version of myself has been accepting method parameters with the same names as class members accept with a 'p' before the name.

This is quite a bad idea for a number of reasons. Probably the biggest is that it breeds confusion and bugs over forgetting to put the 'p' there or forgetting to leave it off, and then not noticing whether or not it's there when you're reading through the code again later.

Perhaps the biggest problems with Hungarian notation come from what it's original root must have been. That root is taking things that should be spelled out in simple English, and reducing them to terse, hard to remember symbols which often must be looked up. This increases coding time, increases debugging time, and is merely the product of the dreams of those inexperienced in coding.

Really "good" Hungarian notation can get variable names like "rgfpBalance" spread all over your code. And I kid you not, thats a perfectly typical name by that system.

I have a bigger beef with "systems Hungarian", which is basically a worse version of a classic coding mistake. In typical code:

        // an integer
        int variable;

And in "systems Hungarian":

        int iVariable;

As you can see, I hate Hungarian notation. This is probably the only occasion on which I have denied the right of balance. That's where I spend the better part of the post ranting about something, and then try to balance it out with some kind of optimistic statement about the pro's of what I'm ranting about. But this time I'm going to leave it without any form of balance, as I feel such behavior is inexcusable. And yes I'm getting it out of my code, so don't accuse me of being hypocritical.


No comments:

Post a Comment