Breadcrumbs

Hungarian Notation in C++

Hungarian Notation is a hated or loved thing between C++ developers. Programmers who learned without it, hate it. Programmers who generally learned with Hungarian Notation love it. As the spark of a holy war, Hungarian Notation has its own disadvantages and advantages that make it such a controversy.

What is Hungarian Notation?

As far as I know, Hungarian Notation has no relationship with Hungarian, but was actually created by Charles Simonyi from Microsoft. Hungarian Notation is a naming convention that allows the programmer to determine the type and use of a variable by an identifier.

Hungarian Notation is generally used in Windows Programming (Visual Basic, C++, etc.) and may prove useful if you're working with Windows code. Although it's not generally applied to anything other than Windows coding, it still can easily be adapted to fit any programming environment.

A complete Object Orientated language would have no purpose for Hungarian Notation. In an Object Orientated language there are no primitive types, therefore causing Hungary Notation to render useless. Although C++ developers may deny it for life, C++ is not a pure Object Orientated Language. Even though it has OOP abilities, it is a hybrid language meaning you can mix objects with primitive data-types.

How It Works

Hungarian Notation is a very easy concept to grasp; it only requires a little memorization before you can get it down pat. There are a few variations of Hungarian Notation, but all are quite similar so the choice is up to you.

First let's take some simple C++ code:

Code: C++

#include <iostream>
int main() {
    using std::cout;
    int number = 2;
    cout << "The value of number is: " << number;
    number = 3;
    cout << "The new value of number is: " << number;
    return 0;
}

Above you see some simple code, but a great code block to demonstrate the use of Hungarian Notation. Using Hungarian Notation our code would change to:

Code: C++

#include <iostream>
int main() {
    using std::cout;
    int iNumber = 2;
    cout << "The value of number is: " << iNumber;
    iNumber = 3;
    cout << "The new value of number is: " << iNumber;
    return 0;
}

The preceding code only has one change to it, the variable name. As you see iNumber is of the integer type. The "i" preceding "Number" signifies that this variable is an integer. At first you may say "How will this help me?" Think about when you're working with longer code. Would it not help to know what your variables do, so you don't have to look back at your code to figure it out? Hungarian Notation enables you to do this, and it's an easy practice.

Another example might be a variable titled "string" and of the string type. Applying Hungarian Notation to it would make it become strString.

Here are some other prefixes used in commonly in C++:

Data Type Prefix
boolean b
character char
string str
short integer si
integer i
long integer li
float f
double d

These are only a few of the prefixes for the data types. There are many more data types and many more prefixes for each one. You can even apply your own; Hungarian Notation is only a concept, not a set of strict rules.

Some programmers also take Hungarian Notation a step further using pre-prefixes on their identifiers. These would designate objects as unsigned, an array, a pointer, a reference, static, etc.

Data Type Prefix
unsigned u
static s
array rg
pointer p
reference ref

These pre-prefixes are placed before the usual prefixes. For example:

Code: C++

unsigned int uintBooks;

At times these pre-prefixes can make Hungarian Notation much more confusing. If you find it difficult then just dismiss the pre-prefixes in your coding. Again Hungarian Notation is only a concept.

Hungarian Notation isn't limited to only C++, but many other programming languages who adapt similar aspects. Visual Basic is a good programming language for Hungarian Notation, especially due to its method for declaring variables. Here are some prefixes for Visual Basic:

Data Type Prefix
boolean bln
byte byt
currency cur
date dte
double dbl
integer int
long lng
object obj
single sng
string str
variant vnt

Similar to C++, Visual Basic prefixes can vary depending on the programmer.

Hungarian Notation is easier than it seems and provides an easy to grasp concept that improves your programming. To find the set of prefixes that works best with your coding style just make your own; the prefixes that you designate will always be better than the standard ones. So now go out and start using the fantastic concept of Hungarian Notation.