Hungarian notation

Source: Wikipedia, the free encyclopedia.

Hungarian notation is an

function indicates its intention or kind, or in some dialects, its type. The original Hungarian notation uses only 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 Microsoft Office applications. When the Microsoft Windows division adopted the naming convention, they based it on the actual data type, and this convention became widely spread through the Windows API
; 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, 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

CamelCase
). Otherwise the case of this character denotes scope.

History

The original Hungarian notation was invented by

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.[citation needed
]

Simonyi's paper on the notation referred to prefixes used to indicate the "type" of information being stored.[3][4] 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 division of Microsoft. Systems Hungarian developed later in the Microsoft Windows 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).[4]

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 and cross-site scripting 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:[3]

  • pX 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

BASIC, name$ names a string and count% names an integer
. 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

The mnemonics for pointers and

arrays
, 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 for use with the C language, in particular for Microsoft Windows, and its use remains largely confined to that area. In particular, use of Hungarian notation was widely evangelized by Charles Petzold's "Programming Windows", the original (and for many readers, the definitive) book on Windows API 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

scope of a variable, optionally separated by an underscore.[5][6]
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 code using jQuery, a $ prefix is often used to indicate that a variable holds a jQuery object (versus a plain DOM object or some other value).[7]

Advantages

(Some of these apply to Systems Hungarian only.)

Supporters argue that the benefits of Hungarian Notation include:[3]

  • 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 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 (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.
  • 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:

Notable opinions

  • Robert Cecil Martin (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.[10]

  • Linus Torvalds (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.[11]

  • 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.[12]

  • Bjarne Stroustrup (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.[13]

  • intelligent code completion system] won't tell you 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.[4]

  • Microsoft's Design Guidelines[14] 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 6 and earlier. These Design Guidelines are silent on the naming conventions for local variables inside functions.

See also

References

  1. ^ "Oral History of Charles Simonyi" (PDF). Archive.computerhistory.org\accessdate=5 August 2018. Archived (PDF) from the original on 2015-09-10.
  2. ^ Rosenberg, Scott (1 January 2007). "Anything You Can Do, I Can Do Meta". MIT Technology Review. Retrieved 21 July 2022.
  3. ^
    Microsoft Corp.
  4. ^ a b c Spolsky, Joel (2005-05-11). "Making Wrong Code Look Wrong". Joel on Software. Retrieved 2005-12-13.
  5. ^ "Mozilla Coding Style". Developer.mozilla.org. Retrieved 17 March 2015.
  6. ^ "Webkit Coding Style Guidelines". Webkit.org. Retrieved 17 March 2015.
  7. ^ "Why would a JavaScript variable start with a dollar sign?". Stack Overflow. Retrieved 12 February 2016.
  8. (PDF) from the original on 2011-05-01.
  9. ^ "Make an app for any task - FileMaker — An Apple Subsidiary". Filemaker.com. Retrieved 5 August 2018.
  10. .
  11. ^ "Linux kernel coding style". Linux kernel documentation. Retrieved 9 March 2018.
  12. .
  13. ^ Stroustrup, Bjarne (2007). "Bjarne Stroustrup's C++ Style and Technique FAQ". Retrieved 15 February 2015.
  14. ^ "Design Guidelines for Developing Class Libraries: General Naming Conventions". Retrieved 2008-01-03.

External links