Hungarian Notation

   

One of the early developers of Windows was a Hungarian who used a naming convention that has been adopted by most developers of the Windows operating system and by many Windows software developers.  In this notation, the name of a variable or an object name is preceded by an (standard) abbreviation of its type. 

 

For example, the name nCount represents an integer counter, pNode represents a pointer to a Node object, fAmount is a floating point amount, and strName is a name represented as a string object.  Sometimes, prefixes are combined.  For example, lpszTitle is a long pointer to a zero-delimited string (array of characters).

 

The type abbreviation is in lower case while each word in the rest of the name begins with an upper case character.  The name of the variable itself follows the abbreviation of its type.  The non-type portion of the name begins with an uppercase character, and it should be descriptive of the entity it names.

 

     This naming convention for variables has the advantage of reminding the programmer/maintainer of the data type associated with a particular data-item name without having to find the place where the variable was declared.  Additionally, it may help to reduce compile-time errors resulting from the incorrect usage of a variable in a situation in which it is not appropriate.

 

     Some examples of identifier names using the Hungarian notation appear in the following (non-exhaustive) list.

 

pGismo

Pointer to a Gismo object

bAnswer

Boolean answer

dBalance

Double precision float balance

strCourseTitle

String course title

nChildren

Integer number of children

menuMain

Main menu

fstrmTransactionFile

fstream transaction file object

nPartNumArray

Array of integer part numbers

fPriceOfHouse

Float price of a house

szCharArray

A 0-delimited array of characters

 

     Hungarian notation is typically used with identifier names representing variables.  Function names are typically composed of one or more words, each of which starts with an upper case letter with no type prefix.  Examples of function names are GetBalance, DisplayList, BinarySearch, and Format. 

 

     Named constants are often given uppercase-only names.  Examples are TAB, ZERO, MAX_ENTRIES, and NULL.

 

     The use of Hungarian notation is required in this course because it is in common use in many Windows programming environments, because you will likely encounter it when reading others’ C++ code, and just because it is a good idea to indicate the type of value a variable represents as a part of its name.