Tuesday, April 11, 2006

I Hate Hungrian Notation

I hate Hungarian Notation. It's true. I'm willing to stand behind this one. When I'm reading code, the alphabet soup at the start of the variable names just slows me down. Moreover, it's often misleading. Take, this example from a real piece of code:

string m_pcResourceName;

Now, please correct me if I'm wrong, but I believe that notation is saying that the variable is a char*. But it's not. It's a std::string. While std::string and char* are used for effectively the same things -- storing string values -- their usage is completely different.

Now, you can say that this is just the bad habits of one person -- that he should've called it m_strResourceName, or some such. But how do you know what string library he's referring to. Heck, traditional Hungarian Notation insists on using an "o" in front of all objects. But, that's not meaningful or useful. The problem is, when you are doing object-oriented programming, your world is full of new types. A full game project could have hundreds of types. Do you call them all "o"? Or are you going to come up with some clever acronym for every single type in the entire project? And will everyone remember what the heck all those acronyms mean?

Variables should always be named very clearly, in a way that makes their purpose readily transparent. In the event that I need to know the exact type of a variable, there happens to be a very good, authoritative place to find out: The variable declaration. It is always right and meaningful, unlike Hungarian Notation.

Old Comments