Hungarian notation
Encyclopedia : H : HU : HUN : Hungarian notation
Hungarian notation is a naming convention in computer programming, in which the name of an object indicates its type or intended use. There are two types of Hungarian notation: Systems Hungarian notation and Apps Hungarian notation.
It was designed to be language-independent, and found its first major use with the BCPL programming language. Because BCPL has no data types other than the machine word, nothing in the language itself helps a programmer remember variables' types. Hungarian notation aims to remedy this by providing the programmer with explicit knowledge of each variable's data type.
In Hungarian notation, a variable name starts with one or more lower-case letters which are mnemonics for the type or purpose of that variable, followed by whatever the name the programmer has chosen; this last part is sometimes distinguished as the given name. The first character of the given name can be capitalised to separate it from the type indicators (see also CamelCase). Otherwise the case of this character denotes scope.
Systems vs. Apps Hungarian
Where Systems notation and Apps notation differ is in the purpose of the prefixes.In Systems Hungarian notation, the most common form, the prefix encodes the actual data type of the variable. For example:
- ulAccountNum : variable is an unsigned long integer
- szName : variable is a zero-terminated string
- rwPosition : variable represents a row
- usName : variable represents an unsafe string, which needs to be translated by some function to make it safe (e.g. see Cross site scripting)
- strName : Variable represents a string containing the name, but does not specify how that string is implemented.
It is possible for code using Apps Hungarian notation to sometimes contain Systems Hungarian when describing variables that are defined solely in terms of their type.
Relation to sigils
In some programming languages, a similar notation now called sigils is built into the language and enforced by the compiler. For example, in BASIC,name$ names a string and count% names an integer, and in Perl, $name refers to a scalar value while @namelist refers to a list of values. Sigils have the notable advantages over Hungarian notation that they implicitly define the type of the variable without need for redundant declaration, and are also checked by the compiler, preventing omission and misuse.On the other hand, such systems are in practice less flexible than Hungarian notation, typically defining only a few different types — the lack of an adequate number of different easy-to-remember symbols obstructs more extensive use. In addition, although it has not been done, it is feasible to construct a static-checking tool which could statically verify the presence and correctness of Hungarian prefixes.
Examples
- bBusy : boolean
- cApples : count of items
- dwLightYears : double word
- fBusy : boolean (flag)
- nSize : integer
- iSize : integer
- fpPrice: floating-point
- dbPi: double
- pFoo : pointer
- rgStudents : array, or range
- szLastName : zero-terminated string
- u32Identifier : unsigned 32-bit integer
- stTime : clock time structure
- pszOwner : pointer to zero-terminated string
- rgfpBalances : array of floating-point values
- hwndFoo : handle to a window
- lpszBar : long pointer to a zero-terminated string
- m_nWheels : member of a class, integer
Criticisms
Supporters argue that the benefits include:[Hungarian Notation, Charles Simonyi, Microsoft Corporation]
- The variable type can be seen from its name
- Variable names can be easy to remember from knowing just their types.
- It leads to more consistent variable names
- Deciding on a variable name can be a mechanical, and thus quick, process
- Innappropriate type casting and operations using incompatible types can be detected easily
- It encourages the use of poor naming practices. For example it becomes confusing when it is used to represent several properties, as in:
- :a_crszkvc30LastNameCol : constant reference function argument, holding contents of a database column of type varchar(30) called LastName that was part of the table's primary key
Origin
The original Hungarian notation, which would now be called Apps Hungarian, was invented by Charles Simonyi, a programmer working at Xerox, who later became Chief Architect at Microsoft.The notation is named for Simonyi's nation of origin. Hungarian people's names are "reversed" compared to most other European names: the family name precedes the given name. For example, the anglified name "Charles Simonyi" in Hungarian was originally "Simonyi Károly". In the same way the type name precedes the "given name" in Hungarian notation rather than the more natural, to most Europeans, Smalltalk "type last" naming style e.g. aPoint and lastPoint. This latter naming style was most common at Xerox PARC during Simonyi's tenure there. It may also be inspired by play on the name of an unrelated concept, Polish notation.
The name Apps Hungarian was coined since the convention was used in the applications division of Microsoft. Systems Hungarian developed later in the Microsoft Windows development team. Simonyi's paper referred to prefixes used to indicate the "type" of information being stored; the word "type" was misunderstood to mean "data type", and systems of prefixes started to be devised on the basis of this misunderstanding.
The term Hungarian notation is memorable for many people because the strings of unpronounceable consonants vaguely resemble the consonant-rich orthography of some Eastern European languages despite the fact that Hungarian is a Finno-Ugric, not a Slavic, language, and so is rather richer in vowels. For example the zero-terminated string prefix "sz" is also a letter in the Hungarian alphabet.
References
External links
- [Meta-Programming: A Software Production Method]. Charles Simonyi, December 1976 (PhD Thesis)
- [Charles Simonyi's explanation of Hungarian Notation]
- [Apps Hungarian Notation]
- [MSDN Windows 98 Conventions and Data Types]
- [HTML version of Doug Klunder's paper]
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
