Minh’s Notes

Human-readable chicken scratch

Minh Nguyễn
May 15th, 2005


Face it, you’re wrong

This is one of those blog entries that I’ve been procrastinating for awhile. It’s about programming, so if you’re not into computers and the like, you’d best scroll down below this entry now. Go on. I won’t begrudge you.

Joel Spolsky is a software developer in New York, someone whose articles I’ve linked to plenty of times in the course of making a point, but whom have never actually mentioned. Seemingly as often as I update my blog – in spurts, that is – in spurts – he publishes a really eye-opening story relating programming with The Real World™.

So enough with the boring introduction: “Making Wrong Code Look Wrong,” his latest article, is one that only aspiring programmers would ever want to read.

This is the real art: making robust code by literally inventing conventions that make errors stand out on the screen.

So now I’ll walk you through a little example, and then I’ll show you a general rule you can use for inventing these code-robustness conventions, and in the end it will lead to a defense of a certain type of Hungarian Notation, probably not the type that makes people carsick, though, and a criticism of exceptions in certain circumstances, though probably not the kind of circumstances you find yourself in most of the time.

But if you’re so convinced that Hungarian Notation is a Bad Thing and that exceptions are the best invention since the chocolate milkshake and you don’t even want to hear any other opinions, well, head on over to Rory’s and read the excellent comix instead; you probably won’t be missing much here anyway; in fact in a minute I’m going to have actual code samples which are likely to put you to sleep even before they get a chance to make you angry. Yep. I think the plan will be to lull you almost completely to sleep and then to sneak the Hungarian=good, Exceptions=bad thing on you when you’re sleepy and not really putting up much of a fight.

I first learned about this ridiculous thing called Hungarian Notation while learning Visual Basic 6, from a book called Visual Basic 6 from the Ground Up. (Ick… Visual Basic… it wasn’t the highlight of my computing career.)

Here’s what the author, Gary Cornell, says about Hungarian:

Some programmers like to use the convevntion of adding a lowercase prefix to variables to indicate what type they are. … This is usually called “Hungarian Notation.” … I don’t use Hungarian notation for variables in my code, preferring code that reads more easily in English. I simply find sngInterestRate to be a less appealing name than InterestRate. … Certainly, almost everybody uses “Hungarian style” prefixes for the names of controls as you saw in Chapter 4: txtFirstName, for example, as the name of a text box. The following table summarizes the Hungarian prefixes for the basic data types:

He goes on to include a table with the basic Visual Basic types – String, Boolean, Integer, etc. – and their prefixes. According to Spolsky, this would be the much-hated “Systems Hungarian,” the source of great misunderstanding over Hungarian Notation.

As it turns out, I got used to the convention quite rapidly, because everything in MSVB involved Systems Hungarian. As Cornell correctly noted, every control on a form was named with the trademark two- or three-letter prefix. Perhaps it was a bit useful, because there were simply so many types to keep track of – one for every control. But it was just a pain to type out.

Spolsky’s points out that people dislike Hungarian because they misunderstand it. Charles Simonyi (Simonyi Károly), the developer of the original Hungarian Notation, intended that the prefixes would indicate things like whether the String variable was escaped or not, or what purpose the variable served. This can cut down on a veritable ton of comments in the code, as I later discovered. But Simonyi quite consistently used the word “type” – instead of “characteristic” or “purpose” or “kind” – in his paper to describe the function of a variable, so the people who popularized his convention ended up popularizing the wrong thing: what came to be known as Systems Hungarian.

Sophomore year, when I first told Mr. Hoar that I used Hungarian Notation, he had no clue what I meant. It turns out that he was only familiar with the capitalization rule of Hungarian, which he called “Hump Notation,” but is more commonly known as “mixedCase.”

Now, I try to use the original, more useful form of Hungarian, known as “Apps Hungarian.” As I mentioned before, it cuts down on a lot of comments, because the prefixes that I include in each variable name actually tell me something about the variable that I’d otherwise need to put in a small comment to the side, something that the IDE couldn’t readily tell me.

Spolsky’s main argument (before he digresses into a discussion exception-based programming) is that using a convention like Apps Hungarian can actually cut down on errors. Security-sensitive information like whether a string is encoded or not is right there in front of you, not shoved over to the side in a gray, italicized comment. It can also help out with complex programs in which you could have a hierarchy of variables, each with its own special purpose in life: by using a consistent set of brief prefixes or mneumonic devices in the variable (or function) name, you also cut down on the required length of a variable name.

So instead of having a variable _hungarian_date_format_re1, which somewhat resembles German on a bad day, you can have _redHungarian, since the script I took it from defines quite a few of these date format regular expressions. The underscore is there because it’s a global variable in Python. Although languages like Python now allow underscores as part of a variable name, seemingly defeating the purpose of Hungarian, the underscore is still a hard character to type, compared to the Shift key.

And since I spent the whole day sifting through nearly all the language editions of Wikipedia, I happen to know that hu is the ISO code for Hungary and the Hungarian language, I’ll simply call the variable _redHu.

(Read the rest of Joel Spolsky’s article, and let me know if I missed or misunderstood something.)

  1. A regular expression matching the Hungarian date format (no kidding!), taken from the source of Mark Pilgram’s Universal Feed Parser, written in Python. ()



  1. I don't know the difference between a backslash\ and a forward slash/. I certainly don't understand this stuff. I'm goona go pop off and oppres your ancestors now. Pip pip.

    -The Doctor