What is Hungarian Notation (HN)?
HN is a naming convention in computer programming, in which the name
of a variable indicates its type or intended use.
HN was designed to be language-independent, and found its first major use
with the BCPL programming language, which is not strongly typed.
However, the HN that lots of people hate is used in more strongly
typed languages like C, C++ and Java. This version of HN is referred
to as
Apps Hungarian by Wikipedia.
HN was invented by Charles Simonyi, a programmer who worked at Xerox
PARC circa 1972-1981, and who later became Chief Architect at
Microsoft. He is actually Hungarian but that is just an amazing coincidence,
it is not why the notation is called HN.
The identifier names produced by HN are unreadable and
unpronoucable. They vaguely resemble the consonant-rich
orthography of some
Eastern European languages.
This made some people say "I can't read
that variable name, it may as well have been written in Hungarian!".
These people also probably had in mind an old system used on Hewlett
Packard calculators called Reverse Polish Notation, designed to make
data entry quicker but at the expense of the more easily understood
and conventional infix notation.
Hungarian versus 'Hungarian Lite'
The Wikipedia article on HN says that Simonyi proposed a version of
HN in which the variable name contained a name that indicated the purpose
of the variable. This is in dispute. Many think that the original proposal
was that the identifier indicated type only and not purpose.
See
the Jim Hyslop and Herb Sutter article from Doctor Dobbs journal.
A weakened form of HN in which the variable can (and does) contain a
name that indicates purpose, it sometimes referred to
as 'Hungarian Lite'. This form of the notation often relaxes
the rules on prefixes and settles on a much smaller set in which
the most commonly observed prefixes are 'sz' for zero-terminated char
array and 'b' for boolean.
Hungarian Lite is sometimes attributed to Charles Petzold, also of Micro$oft.
He defends his notation and also explains why he no longer uses it,
in his blog article
The Demise of Hungarian Notation.
The convention of using a trailing underscore or a leading m_ on a variable
name to indicate scope (in this case a member variable as opposed
to a local variable) is not to be confused with Hungarian Lite.
Indicating scope is quite different from indicating type.
Why am I ranting about Hungarian Notation?
You would have thought that HN would have died out
years ago. Well, I think that's mostly true but there are still
a few reasons why you will come across it, even now:
-
Every now and then one comes across a project that is still going
that was started years ago when HN was popular. The code will be liberally
sprinkled with HN, bring all those horrible memories back again.
Of course, the Golden Rule of source code modification must be
observed; when making modifications retain the original style/conventions,
hence HN lives on and new HN code gets written.
-
It still gets used by Micro$ofties every now and then,
even on new projects in newer, strongly typed languages (e.g Java).
-
If one has to modify any C or C++ code that has been exclusively for
a Micro$oft environment, even relatively modern code,
then it will be full of HN.
This is because the Micro$oft type names and APIs use HN.
Hence, I am ranting about HN because it lives on and every now
and then I come across it and am forced to read it, and sometimes
write it.
Plus, of course, it is another invention by Micro$oft that has
inflicted great harm on the industry so it is another chance to
have a go at them.
Why I hate Hungarian Notation
-
The names are unreadable and unpronouncable.
This makes the code hard to read and hard to talk about.
The uglyness is why some people refer to the prefixes as warts.
Hence you will hear some people talking about Hungarian Warts.
-
HN violates the principle of avoiding encoding in variable names.
Encoded names require deciphering. FORTRAN forced it by basing type
on the first letter, making the first letter a `code' for the type.
HN takes this to a whole new level.
Of course, you can get used to anything,
but why create an artificial learning curve for new hires?
-
It is another reminder of the dominance of Micro$oft.
Their influence is everywhere and you see it in code that
employs HN.
-
The code is probably lying to you.
The use of HN does not force the programmer to go back and change
the name of a variable and all occasions where it is used when the
type changes. It just means the programmer now has the additional
job of trying to maintain the ugly name. Since the compiler does not
force him to do this he will most likely forget or not bother.
-
The wart is a distraction when you try to think about the variables
purpose. You can try mentally subtracting the wart but this only works
if Hungarian Lite is being used, and even then it is hard work.
-
HN was considered to be pretty important back in the Windows C API,
when everything was an integer handle or a long pointer or a void
pointer, or one of several implementations of "string" (with different
uses and attributes). In modern languages we have much richer type
systems. There is a trend toward smaller classes and shorter functions
so that one can always see the point of declaration of each variable
they're using.
Modern languages don't need type encoding. Objects are strongly (and
often dynamically) typed, and editing environments have advanced.
Occasionally you will see C++ code from a Micro$oft environment
or coded by a Micro$oftie where every class name begins with 'C',
structs that begin with 'S' and typedefs that begin with 'T'.
This is unecessary and serves merely to obfuscate.
-
It doesn't work in the presence of templates (C++) or generics (Java).
-
It is a tip-off that the code you are about to read is probably old
and horrible and may be Micro$oft-specific (often for no good reason).
This is because HN has been largely discredited so tends to be seen
in older code and is due to Micro$oft domination.
-
The Evil has propagated far beyond the C language at which
it was originally aimed. It is now used in object oriented languages
and even when naming database tables!
(See this forum entry).
I'm not the only one who doesn't like HN.
See these links: