Hungarian Notation
   HOME

TheInfoList



OR:

Hungarian notation is an
identifier naming convention In computer programming, a naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types, functions, and other entities in source code and documentation. Reasons for using a na ...
in
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, in which the name of a
variable Variable may refer to: * Variable (computer science), a symbolic name associated with a value and whose associated value may be changed * Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many ...
or
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-oriente ...
indicates its intention or kind, and in some dialects its type. The original Hungarian notation uses intention or kind in its naming convention and is sometimes called Apps Hungarian as it became popular in the Microsoft Apps division in the development of Word, Excel and other apps. As the Microsoft Windows division adopted the naming convention, they used the actual data type for naming, and this convention became widely spread through the
Windows API The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces (APIs) available in the Microsoft Windows operating systems. The name Windows API collectively refers to several different platform implementations th ...
; this is sometimes called Systems Hungarian notation. Hungarian notation 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 A word is a basic element of language that carries an objective or practical meaning, can be used on its own, and is uninterruptible. Despite the fact that language speakers often have an intuitive grasp of what a word is, there is no conse ...
, 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 a group of lower-case letters which are
mnemonic A mnemonic ( ) device, or memory device, is any learning technique that aids information retention or retrieval (remembering) in the human memory for better understanding. Mnemonics make use of elaborative encoding, retrieval cues, and imag ...
s for the type or purpose of that variable, followed by whatever name the programmer has chosen; this last part is sometimes distinguished as the ''given name''. The first character of the given name can be capitalized to separate it from the type indicators (see also
CamelCase Camel case (sometimes stylized as camelCase or CamelCase, also known as camel caps or more formally as medial capitals) is the practice of writing phrases without spaces or punctuation. The format indicates the separation of words with a single ...
). Otherwise the case of this character denotes scope.


History

The original Hungarian notation was invented by
Charles Simonyi Charles Simonyi (; hu, Simonyi Károly, ; born September 10, 1948) is a Hungarian-American software architect. He started and led Microsoft's applications group, where he built the first versions of Microsoft Office. He co-founded and led Int ...
, a programmer who worked at
Xerox PARC PARC (Palo Alto Research Center; formerly Xerox PARC) is a research and development company in Palo Alto, California. Founded in 1969 by Jacob E. "Jack" Goldman, chief scientist of Xerox Corporation, the company was originally a division of Xero ...
circa 1972–1981, and who later became Chief Architect at
Microsoft Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washin ...
. The name of the notation is a reference to Simonyi's nation of origin, and also, according to
Andy Hertzfeld Andrew Jay Hertzfeld (born April 6, 1953) is an American software engineer and innovator who was a member of the original Apple Macintosh development team during the 1980s. After buying an Apple II in January 1978, he went to work for App ...
, because it made programs "look like they were written in some inscrutable foreign language". Hungarian people's names are "reversed" compared to most other European names; the family name precedes the given name. For example, the anglicized name "Charles Simonyi" in Hungarian was originally "Simonyi Károly". In the same way, the type name precedes the "given name" in Hungarian notation. The similar Smalltalk "type last" naming style (e.g. aPoint and lastPoint) was common at Xerox PARC during Simonyi's tenure there. Simonyi's paper on the notation referred to prefixes used to indicate the "type" of information being stored. His proposal was largely concerned with decorating identifier names based upon the semantic information of what they store (in other words, the variable's ''purpose''). Simonyi's notation came to be called Apps Hungarian, since the convention was used in the
applications Application may refer to: Mathematics and computing * Application software, computer software designed to help the user to perform specific tasks ** Application layer, an abstraction layer that specifies protocols and interface methods used in a c ...
division of Microsoft. Systems Hungarian developed later in the
Microsoft Windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
development team. Apps Hungarian is not entirely distinct from what became known as Systems Hungarian, as some of Simonyi's suggested prefixes contain little or no semantic information (see below for examples).


Systems Hungarian vs. Apps Hungarian

Where Systems notation and Apps notation differ is in the purpose of the prefixes. In Systems Hungarian notation, the prefix encodes the actual data type of the variable. For example: *lAccountNum : variable is a ''long integer'' ("l"); *arru8NumberList : variable is ''an array of unsigned 8-bit integers'' ("arru8"); *bReadLine(bPort,&arru8NumberList) : function with a byte-value return code. *strName : Variable represents a string ("str") containing the name, but does not specify how that string is implemented. Apps Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable's purpose is, or what it represents. *rwPosition : variable represents a ''row'' ("rw"); *usName : variable represents an ''unsafe string'' ("us"), which needs to be "sanitized" before it is used (e.g. see
code injection Code injection is the exploitation of a computer bug that is caused by processing invalid data. The injection is used by an attacker to introduce (or "inject") code into a vulnerable computer program and change the course of execution. The re ...
and
cross-site scripting Cross-site scripting (XSS) is a type of security vulnerability that can be found in some web applications. XSS attacks enable attackers to inject client-side scripts into web pages viewed by other users. A cross-site scripting vulnerability m ...
for examples of attacks that can be caused by using raw user input) *szName : variable is a ''zero-terminated string'' ("sz"); this was one of Simonyi's original suggested prefixes. Most, but not all, of the prefixes Simonyi suggested are semantic in nature. To modern eyes, some prefixes seem to represent physical data types, such as sz for strings. However, such prefixes were still semantic, as Simonyi intended Hungarian notation for languages whose type systems could not distinguish some data types that modern languages take for granted. The following are examples from the original paper: * p''X'' is a pointer to another type ''X''; this contains very little semantic information. * ''d'' is a prefix meaning difference between two values; for instance, ''dY'' might represent a distance along the Y-axis of a graph, while a variable just called ''y'' might be an absolute position. This is entirely semantic in nature. * ''sz'' is a null- or zero-terminated string. In C, this contains some semantic information because it is not clear whether a variable of type ''char*'' is a pointer to a single character, an array of characters or a zero-terminated string. * ''w'' marks a variable that is a word. This contains essentially no semantic information at all, and would probably be considered Systems Hungarian. * ''b'' marks a byte, which in contrast to w might have semantic information, because in C the only byte-sized data type is the ''char'', so these are sometimes used to hold numeric values. This prefix might clear ambiguity between whether the variable is holding a value that should be treated as a character or a number. While the notation always uses initial lower-case letters as mnemonics, it does not prescribe the mnemonics themselves. There are several widely used conventions (see examples below), but any set of letters can be used, as long as they are consistent within a given body of code. 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
sigil A sigil () is a type of symbol used in magic. The term has usually referred to a pictorial signature of a deity or spirit. In modern usage, especially in the context of chaos magic, sigil refers to a symbolic representation of the practitioner ...
s is built into the language and enforced by the compiler. For example, in some forms of BASIC, name$ names a string and count% names an
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the languag ...
. The major difference between Hungarian notation and sigils is that sigils declare the type of the variable in the language, whereas Hungarian notation is purely a naming scheme with no effect on the machine interpretation of the program text.


Examples

*bBusy : boolean *chInitial :
char Char may refer to: People *Char Fontane, American actress *Char Margolis, American spiritualist * René Char (1907–1988), French poet *The Char family of Colombia: ** Fuad Char, Colombian senator ** Alejandro Char Chaljub, mayor of Barranquilla ...
*cApples : count of items *dwLightYears : double
word A word is a basic element of language that carries an objective or practical meaning, can be used on its own, and is uninterruptible. Despite the fact that language speakers often have an intuitive grasp of what a word is, there is no conse ...
(Systems) *fBusy : flag (or
float Float may refer to: Arts and entertainment Music Albums * ''Float'' (Aesop Rock album), 2000 * ''Float'' (Flogging Molly album), 2008 * ''Float'' (Styles P album), 2013 Songs * "Float" (Tim and the Glory Boys song), 2022 * "Float", by Bush ...
) *nSize :
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the languag ...
(Systems) or count (Apps) *iSize :
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the languag ...
(Systems) or index (Apps) *fpPrice : floating-point *decPrice : decimal *db Pi :
double A double is a look-alike or doppelgänger; one person or being that resembles another. Double, The Double or Dubble may also refer to: Film and television * Double (filmmaking), someone who substitutes for the credited actor of a character * ...
(Systems) *p
Foo The terms foobar (), foo, bar, baz, and others are used as metasyntactic variables and placeholder names in computer programming or computer-related documentation. - Etymology of "Foo" They have been used to name entities such as variables, f ...
: pointer *rgStudents : array, or range *szLastName : zero-terminated string *u16Identifier : unsigned 16-bit
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the languag ...
(Systems) *u32Identifier : unsigned 32-bit
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the languag ...
(Systems) *stTime : clock time structure *fnFunction : function name The mnemonics for pointers and
arrays An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
, which are not actual data types, are usually followed by the type of the data element itself: *pszOwner : pointer to zero-terminated string *rgfpBalances : array of floating-point values *aulColors : array of unsigned long (Systems) While Hungarian notation can be applied to any programming language and environment, it was widely adopted by
Microsoft Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washin ...
for use with the C language, in particular for
Microsoft Windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
, and its use remains largely confined to that area. In particular, use of Hungarian notation was widely evangelized by
Charles Petzold Charles Petzold (born February 2, 1953) is an American programmer and technical author on Microsoft Windows applications. He is also a Microsoft Most Valuable Professional and was named one of Microsoft's seven Windows Pioneers. Biography He g ...
's ''"Programming Windows"'', the original (and for many readers, the definitive) book on
Windows API The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces (APIs) available in the Microsoft Windows operating systems. The name Windows API collectively refers to several different platform implementations th ...
programming. Thus, many commonly seen constructs of Hungarian notation are specific to Windows: * For programmers who learned Windows programming in C, probably the most memorable examples are the wParam (word-size parameter) and lParam (long-integer parameter) for the WindowProc() function. * hwndFoo : handle to a window * lpszBar : long pointer to a zero-terminated string The notation is sometimes extended in
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
to include the
scope Scope or scopes may refer to: People with the surname * Jamie Scope (born 1986), English footballer * John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution Arts, media, and entertainment * Cinem ...
of a variable, optionally separated by an underscore. This extension is often also used without the Hungarian type-specification: * g_nWheels : member of a global namespace, integer * m_nWheels : member of a structure/class, integer * m_wheels, _wheels : member of a structure/class * s_wheels : static member of a class * c_wheels : static member of a function In
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
code using
jQuery jQuery is a JavaScript library designed to simplify HTML DOM tree traversal and manipulation, as well as event handling, CSS animation, and Ajax. It is free, open-source software using the permissive MIT License. As of Aug 2022, jQuery is u ...
, a $ prefix is often used to indicate that a variable holds a jQuery object (versus a plain DOM object or some other value).


Advantages

(Some of these apply to Systems Hungarian only.) Supporters argue that the benefits of Hungarian Notation include: * The symbol type can be seen from its name. This is useful when looking at the code outside an integrated development environment — like on a code review or printout — or when the symbol declaration is in another file from the point of use, such as a function. * In a language that uses
dynamic typing In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
or that is untyped, the decorations that refer to types cease to be redundant. In such languages variables are typically not declared as holding a particular type of data, so the only clue as to what operations can be done on it are hints given by the programmer, such as a variable naming scheme, documentation and comments. As mentioned above, Hungarian Notation expanded in such a language ( BCPL). * The formatting of variable names may simplify some aspects of
code refactoring In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the '' factoring''—without changing its external behavior. Refactoring is intended to improve the design, structur ...
(while making other aspects more error-prone). * Multiple variables with similar semantics can be used in a block of code: dwWidth, iWidth, fWidth, dWidth. * Variable names can be easy to remember from knowing just their types. * It leads to more consistent variable names. * Inappropriate type casting and operations using incompatible types can be detected easily while reading code. * In complex programs with many global objects (VB/Delphi Forms), having a basic prefix notation can ease the work of finding the component inside of the editor. For example, searching for the string btn might find all the Button objects. * Applying Hungarian notation in a narrower way, such as applying only for member variables, helps avoid
naming collision A naming collision is a circumstance where two or more identifiers in a given namespace or a given scope cannot be unambiguously resolved, and such unambiguous resolution is a requirement of the underlying system. Example: XML element names I ...
. * Printed code is more clear to the reader in case of datatypes, type conversions, assignments, truncations, etc.


Disadvantages

Most arguments against Hungarian notation are against ''Systems'' Hungarian notation, not ''Apps'' Hungarian notation. Some potential issues are: * The Hungarian notation is redundant when type-checking is done by the compiler. Compilers for languages providing strict type-checking, such as Pascal, ensure the usage of a variable is consistent with its type automatically; checks by eye are redundant and subject to human error. * Most modern
integrated development environment An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build automation tools ...
s display variable types on demand, and automatically flag operations which use incompatible types, making the notation largely obsolete. * Hungarian Notation becomes confusing when it is used to represent several properties, as in
--> a_crszkvc30LastNameCol: a constant (computer science), constant
reference Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a '' name'' ...
argument, holding the contents of a
database In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases s ...
column LastName of type
varchar A VARCHAR or variable character field is a set of character data of indeterminate length. The term ''varchar'' refers to a data type of a field (or column) in a database which can hold letters and numbers. Varchar fields can be of any size up to a ...
(30) which is part of the table's primary key. * It may lead to inconsistency when code is modified or ported. If a variable's type is changed, either the decoration on the name of the variable will be inconsistent with the new type, or the variable's name must be changed. A particularly well known example is the standard WPARAM type, and the accompanying wParam formal parameter in many Windows system function declarations. The 'w' stands for 'word', where 'word' is the native word size of the platform's hardware architecture. It was originally a 16 bit type on 16-bit word architectures, but was changed to a 32-bit on 32-bit word architectures, or 64-bit type on 64-bit word architectures in later versions of the operating system while retaining its original name (its true underlying type is UINT_PTR, that is, an unsigned integer large enough to hold a pointer). The semantic impedance, and hence programmer confusion and inconsistency from platform-to-platform, is on the assumption that 'w' stands for a two byte, 16-bit word in those different environments. * Most of the time, knowing the use of a variable implies knowing its type. Furthermore, if the usage of a variable is not known, it cannot be deduced from its type. * Hungarian notation reduces the benefits of using code editors that support completion on variable names, for the programmer has to input the type specifier first, which is more likely to collide with other variables than when using other naming schemes. * It makes code less readable, by obfuscating the purpose of the variable with type and scoping prefixes. * The additional type information can insufficiently replace more descriptive names. E.g. sDatabase does not tell the reader what it is. databaseName might be a more descriptive name. * When names are sufficiently descriptive, the additional type information can be redundant. E.g. firstName is most likely a string. So naming it sFirstName only adds clutter to the code. * It's harder to remember the names. * Multiple variables with different semantics can be used in a block of code with similar names: ''dwTmp, iTmp, fTmp, dTmp''. * Placing data type or intent character identifiers as a prefix to the field or variable's Given name subverts the ability, in some programming environments, to jump to a field or variable name, alphabetically, when the user begins typing the name. FileMaker, for example, is one such programming environment. It may be preferable when using one of these programming environments to instead suffix Given names with such identifying characters.


Notable opinions

*
Robert Cecil Martin Robert Cecil Martin (born 5 December 1952), colloquially called "Uncle Bob", is an American software engineer, instructor, and best-selling author. He is most recognized for developing many software design principles and for being a founder of t ...
(against Hungarian notation and all other forms of encoding):
... nowadays HN and other forms of type encoding are simply impediments. They make it harder to change the name or type of a variable, function, member or class. They make it harder to read the code. And they create the possibility that the encoding system will mislead the reader.
*
Linus Torvalds Linus Benedict Torvalds ( , ; born 28 December 1969) is a Finnish software engineer who is the creator and, historically, the lead developer of the Linux kernel, used by Linux distributions and other operating systems such as Android. He also ...
(against Systems Hungarian):
Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged—the compiler knows the types anyway and can check those, and it only confuses the programmer.
* Steve McConnell (for Apps Hungarian):
Although the Hungarian naming convention is no longer in widespread use, the basic idea of standardizing on terse, precise abbreviations continues to have value. Standardized prefixes allow you to check types accurately when you're using abstract data types that your compiler can't necessarily check.
*
Bjarne Stroustrup Bjarne Stroustrup (; ; born 30 December 1950) is a Danish computer scientist, most notable for the invention and development of the C++ programming language. As of July 2022, Stroustrup is a professor of Computer Science at Columbia University ...
(against Systems Hungarian for C++):
No I don't recommend 'Hungarian'. I regard 'Hungarian' (embedding an abbreviated version of a type in a variable name) as a technique that can be useful in untyped languages, but is completely unsuitable for a language that supports generic programming and object-oriented programming — both of which emphasize selection of operations based on the type and arguments (known to the language or to the run-time support). In this case, 'building the type of an object into names' simply complicates and minimizes abstraction.
*
Joel Spolsky Avram Joel Spolsky (born 1965) is a software engineer and writer. He is the author of ''Joel on Software'', a blog on software development, and the creator of the project management software Trello. He was a Program Manager on the Microsoft Exce ...
(for Apps Hungarian):
If you read Simonyi's paper closely, what he was getting at was the same kind of naming convention as I used in my example above where we decided that us meant unsafe string and s meant safe string. They're both of type string. The compiler won't help you if you assign one to the other and Intellisense n_ n_Intelligent_code_completion_system">Intelligent_code_completion.html"_;"title="n_Intelligent_code_completion">n_Intelligent_code_completion_systemwon't_tell_you_ n_Intelligent_code_completion_system">Intelligent_code_completion.html"_;"title="n_Intelligent_code_completion">n_Intelligent_code_completion_systemwon't_tell_you_:wikt:bupkis#English">bupkis._But_they_are_semantically_different._They_need_to_be_interpreted_differently_and_treated_differently_and_some_kind_of_conversion_function_will_need_to_be_called_if_you_assign_one_to_the_other_or_you_will_have_a_runtime_bug._If_you're_lucky._There's_still_a_tremendous_amount_of_value_to_Apps_Hungarian,_in_that_it_increases_collocation_in_code,_which_makes_the_code_easier_to_read,_write,_debug_and_maintain,_and,_most_importantly,_it_makes_wrong_code_look_wrong...._(Systems_Hungarian)_was_a_subtle_but_complete_misunderstanding_of_Simonyi’s_intention_and_practice.
*_Microsoft_ Microsoft_Corporation_is_an_American__multinational__technology_corporation_producing_computer_software,_consumer_electronics,_personal_computers,_and_related_services_headquartered_at_the__Microsoft_Redmond_campus_located_in__Redmond,_Washin_...
's_Design_Guidelines_discourage_developers_from_using_Systems_Hungarian_notation_when_they_choose_names_for_the_elements_in_.NET_class_libraries,_although_it_was_common_on_prior_Microsoft_development_platforms_like_Visual_Basic_(classic).html" ;"title=":wikt:bupkis#English.html" ;"title="Intelligent_code_completion_system.html" ;"title="Intelligent_code_completion.html" ;"title="n Intelligent code completion">n Intelligent code completion system">Intelligent_code_completion.html" ;"title="n Intelligent code completion">n Intelligent code completion systemwon't tell you :wikt:bupkis#English">bupkis. But they are semantically different. They need to be interpreted differently and treated differently and some kind of conversion function will need to be called if you assign one to the other or you will have a runtime bug. If you're lucky. There's still a tremendous amount of value to Apps Hungarian, in that it increases collocation in code, which makes the code easier to read, write, debug and maintain, and, most importantly, it makes wrong code look wrong.... (Systems Hungarian) was a subtle but complete misunderstanding of Simonyi’s intention and practice. *
Microsoft Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washin ...
's Design Guidelines discourage developers from using Systems Hungarian notation when they choose names for the elements in .NET class libraries, although it was common on prior Microsoft development platforms like Visual Basic (classic)">Visual Basic 6 and earlier. These Design Guidelines are silent on the naming conventions for local variables inside functions.


See also

* Leszynski naming convention, Hungarian notation for database development * Polish notation * PascalCase


References


External links


Meta-Programming: A Software Production Method
Charles Simonyi, December 1976 (PhD Thesis)
Hugarian notation - it's my turn now :)
– Larry Osterman's WebLog
Hungarian Notation
(MSDN)
HTML version of Doug Klunder's paperRVBA Naming ConventionsCoding Style Conventions
(MSDN) {{DEFAULTSORT:Hungarian Notation Source code Naming conventions