Bob Moore's Coding Tips

24. What is "Hungarian" notation?

Hungarian notation was invented by Charles Simonyi (a Microsoft employee of Hungarian extraction) and presented in his thesis on meta-programming. Essentially it consists of a naming convention whereby the items named have information about them encoded in their names - hence "meta-programming".   This allows you to do visual type checking, and eliminates a lot of the up-down flick reading required in C code to get from variable declaration to variable usage (free variable declaration in C++ eliminates much of this, but there's all that legacy code to consider).

Where I part company from Simonyi and MS is the degree to which he/they use the notation in a problem-domain way rather than a language-domain way. For instance, take a look at some MS source code : you'll see counter always referred to with the prefix 'n' (which simply means a counter in pure Hungarian), irrespective of whether that counter is declared as an int, an unsigned int, a BYTE, whatever. Similarly, f - meaning flag - may be used to mean a simple BOOL or a UINT array of binary bits. This allows the underlying type to be re-declared without changing the code (not something you do very often) but throws away at a stroke the advantages of visual type-checking (I read code every day).

This is the way I use Hungarian :

<scope prefix>_<modifier><type><CapitalisedVariableName>

The scope prefix is omitted for local-scope variable. So for example :

iCounter Integer counter, local scope
aiSalesFigures An array of integers representing regional sales figures, at local scope.
g_dwLbIndex DWORD listbox index, with app-global scope
mg_byOffset A BYTE offset with module-global scope
m_byMachineNo A class member variable which is a byte containing a machine number
szCustomerName A zero-terminated string containing a customer name, local scope.

I cannot understand why any professional programmer would refuse to use Hungarian, but many do. This mystifies me. Take a look at the sources : you can do it my way, or Microsoft's, I don't care - but I do implore you to use some form of Hungarian. It will save you a lot of time.

For further information, visit this link :

http://www.csci.csusb.edu/doc/hungarian

Back to tips