Hungarian Notation Postmortem: What Went Wrong?
Is Hungarian notation really adjDead? Suggesting a postmortem might be a bit premature, but it could be fun to take a quick dive into what we?ve learned from a really smart Hungarian named Charles Simonyi who tried to find a better way. Anyone who tries to bring some clarity to the chaos they find in their everyday dealings has a lot in common with us developers. Mr. Simonyi is one of us. Let?s hear him out!
What Is Hungarian Notation?
Hungarian notation is one of those topics of great debate in the programming community. You can find just as many intelligent and well-respected proponents as you can find opponents. Likewise, you can find lists about its positive contributions, and you can read articles on why it should never surface. As with most of these more philosophical debates, I find myself stuck in the middle. And when that happens, I find great value in hearing out those with strong opinions about it.
To a non-developer, computer code can appear rather ambiguous and obscure. To a developer, someone else?s computer code can be the same when we fail to be united in our approach. If you?ve spent any amount of time hacking away at a keyboard, writing complex computer instructions, I?m sure you?ll understand the concepts behind Hungarian notation. Mr. Simonyi is simply trying to bring some clarity to the language you?re using with your computer.
How Does It Work?
So, what is Hungarian notation and how does it work? To sum it up, Hungarian notation is a systematic approach to naming variables in our programs, using the type and a given name, in that order. If ?lastName? is a string, think strLastname for its Hungarian notation. Consider this block of pseudocode:
string ln= ?Simonyi?; string fn= ?Charles?; string dn= ln + ?,? + fn; int len = dn.length();
It?s a pretty simple piece of code with a few string variables and an integer. Come on a quick journey with me; let’s say this was line 444 through 448 in 1000+ lines of code. The variable names tell us very little about what we?re storing in them. And when the variable is referenced further down in the code, we may find ourselves scrolling back up to find out what it means. If we used Hungarian notation, along with some better variable names, maybe we?d see something like this:
string sLastname = ?Simonyi?; string sFirstname = ?Charles?; string sDisplayname = sLastname + ?,? + sFirstname; int nLength= sDisplayname .length();
You?ll notice right away that the variable names are clear, their intents and purposes are solidified, and they?re prepended with the correct type (s for string, n for integer). No more guessing, and no more scrolling back to check the type.
That?s it.
Yup, that?s the basic idea behind Hungarian notation. And you can find article after article, opinion piece after opinion piece, and plenty debate all throughout the internets about it.