Hungarian notation is a variable naming convention in programming languages, in which the variable name is prefixed with letter(s) which indicate its type or intended use. In this article, I describe the Hungarian variable naming convention and discuss if we should use Hungarian notation while coding or not.
Charles Simonyi Versus Charles Petzolt
Charles Simonyi, a software architect at Microsoft was the inventor of Hungarian notation. Charles Simonyi proposed that the variable name be prefixed with 'type' information. Simonyi meant 'semantic type' - or what that variable represents - when he used the word 'type'. This later came to be known as Apps Hungarian
For example: rwPosition -- where 'rw' prefix indicates that this is a 'row'.
People mis-interpreted Simonyi's original text and assumed 'type' to mean a 'data type' instead of the 'semantic type'. So the prefixes started indicating the 'data type' of the variable. Charles Petzolt released the immensely popular book: 'Programming Windows' which used this notation. This later came to be known as Systems Hungarian
For example: bBusy -- the prefix 'b' indicated boolean which is a data type.
In Systems Hungarian, as we have seen, the prefix describes the data type of the variable. This information is useless overhead as any modern IDE like Eclipse or NetBeans provides us with this information.
For example: Eclipse IDE provides information about the variable costTable that it is a two dimensional integer array. We do not need the variable name to indicate the 'data type'. The variable name 'costTable' has to indicate what this variable is all about - semantic meaning - from the name we can infer that this holds the 'cost' of some object.
In Apps Hungarian, the emphasis is on 'semantic type' - which is what the variable represents. This information cannot be supplied by IDE or other tools. The programmer is the one to supply this piece of information.
Some examples of variable names enhanced by a 'semantic data type' prefix.
- diffX -- the prefix indicates difference.
- countX -- the prefix indicates count.
- maxX -- the prefix indicates maximum value permitted
If we rewrite the above example using Systems Hungarian, we would get the variable name as: iX - which only indicates that variable X is an integer but does not provide with any information about what kind of data that is.
Here is the original paper by Charles Simonyi: Hungarian Notation
Apps Hungarian is very relevant as it places emphasis on the 'semantic type' of the variable. This information can only be supplied by the programmer and not by IDE or tools. Traditionally 'prefix' was often a single character. However, this restriction can be relaxed so that the code is easier to read and more useful.