The Var Hungarian


I was reading a blog post from one of my favorite software bloggers, Joel Spolsky, and came upon the middle of the article ( where he talks about Hungarian notation.  I was floored because I, too, had been brainwashed by what I thought was the intent of the notation.  It turns out that what everyone thinks it’s for (knowing what kind of variable something is or how a function might work) was not the original intent and use.  It was really for telling you what the variable DID and what it stood for, to make you less likely to make a mistake in coding.  Seriously, read the article, then come back here, I can wait a few minutes….  At the very least, go there and find the section marked “I’m Hungary” and read that.

So, you probably just learned that we’ve bastardized a pretty useful concept because of bad documentation (that’s a story for another day).  But, you’ve also probably gotten a seed in your brain that maybe you can help fix some of the insidious bugs that pop up in your own code.  Or, maybe just a way that you could write code to make it easier to return to sometime later and understand better, or how to train new coders in what your code does.

I’ve always been an advocate of straightforward code.  Nothing too clever (sometimes I do and sometimes it works, and sometimes it doesn’t), but the point is, someone should be able to come by, look at my code, my functions, and the data, and figure out how to muck with it or use it without needing therapy afterwards. 

The latest craze in C# programming is the use of the keyword “var”, which suddenly stands for “there’s no need to actually type ‘int’ even when that’s what a variable is.”  I understand that it’s necessary for queries and can eliminate some duplication of typing of complicated templated types, like Dictionary<List<int>, double>, but some people go too far.  Intellisense can help with the discovery of variable types, but nothing beats looking at code and being able to see what type something really is (especially when you actually know what it is) to be able to understand the code without having to highlight every variable and figure out (then remember what it is).

The same thing can be said of clever C or C++ macros which can just as easily be done with functions.  Seriously, this is what the “inline” keyword was made for and if that doesn’t work for you, then just incur the function call overhead unless you’re writing realtime software or something that’s so time critical that there’s no other way to do it.  Otherwise, every single person who looks at that code (including you six months later) is going to have to search all of the header files wondering where the code is because your IDE (or worse, linux’s gedit) sure as heck won’t tell you.

Simple, straightforward code – your New Year’s resolution.  And mine.