Click here to Skip to main content
13,196,946 members (55,185 online)
Click here to Skip to main content
Add your own
alternative version

Stats

8.9K views
3 bookmarked
Posted 9 May 2017

Hungarian Reigns

, 9 May 2017
Rate this:
Please Sign up or sign in to vote.
Chances are you have heard, or even told someone, not to use the Hungarian Notation naming convention. Chances are, you are using it yourself. Hungarian Notation is the king of naming conventions. Though many call for its demise, it is here to stay.

What is Hungarian Notation?

For those not familiar with Hungarian Notation, it is a naming convention that incorporates an item's datatype, intended use, or other metainformation into the item name.  Many people have a narrow understanding of Hungarian, and discourage its use. But as you will see, Hungarian is more than "strName", and is not going away any time soon.

Pre-History

Hungarian Notation goes back to the dawn of computing, and formal definitions have been around since the 80's. It first became popular for managing typing in languages that were not syntactically typed, and when simple editors, not informative IDE's, were the tools we had.

In languages like C, we can be explicit about variable types with declarations like

int x = 0;

string y = "Hello";

Languages like Basic don't allow explicit typing, and instead use keywords like "let" and "var" for declarations.

let x = 0

var y = "Hello"

Depending on the language, setting x = y could produce an error, or succeed with results like x becoming 'H', or 72 (ASCII code for H).

Early Days

Developers implemented one of the earliest forms of "type-safety" by post-fixing '$' to string variables. This was helpful in the days when there were only two datatypes (number and string), and interpreters and compilers couldn't enforce this.

let x = 0 // x is a number

let y$ = "Hello" // y is a string

For consistancy, the suffix % was also used in some areas to indicate a number. As languages matured and additional datatypes became available, another approach was needed. The earliest form of what would become Hungarian built on the post-fix approach. This was not a stylistic choice, many preferred a type-first approach, but a technical constraint of the BASIC language. The BASIC language was becoming the most popular general purpose computing language around that time.

Early versions of Basic only supported two-character variable names. Though later, versions allowed eight character names, only the first two were used internally - "so" and "some" were the same variable. Adopting the [type] [name] order used in declaration-typed languages like C and FORTRAN ( e.g.,  string :: y)  was not possible - strX  and strY were the same variable internally - st.

The remaining six characters were, I suppose, the first syntactic sugar - helpful for programmers, but meaningless to the computer. Approaches to using the remaining six characters helped give birth to two other eventually pervasive programming concepts - Hungarian Notation and Metadata.

Many schools of thought developed around how to use these six "extraneous"  characters. They can be broken into three main groups - what it is, what type it is, and how it is used. The "what it is" school is basically clear naming - using the extra room to clarify the "real-world" name of the item. A person name goes from "na" to "name". The "type-of" approach uses the room to indicate internal storage type. Name is a string, so - "nastr". The "how-used" had many variations. Some that persist in some forms today are mutability and scope. The constant "name" might be "nacon", or a local variable might be "naloc". This would evolve into things like visibility for variables (i.e., _name for a private variable).

Formalization

Jumping forward many years, the size limitation on variable names had gone away. Clear naming was just part of sound programming practices. Meta-Naming approaches had moved towards the more C-like prefixed and type based approach - strName would be the ordinary form of the example above. Intent based naming had not been abandoned, but was losing favor. As was post-fixing. The advent of object-oriented programming brought both back to some extent. In a Cartesian system, we might find the x and y coordinates of a point named fltXPoint and fltYPoint. Or to indicate the name of a function that returns an integer, we may see intSumFn.

Much of the work around formalizing these conventions took place in the late 80's. The most recognized names in this work are Charles Petzold, Doug Klunder, and Charles Simonyi. Of these, Simonyi had the most influence, if for no other reason that the name Hungarian derived from the Hungarian heritage of Simonyi. Simonyi's influence went well beyond contributing to the name though. Simonyi was the father of MS Word and Office, and standardized the use of Hungarian Notion across that group, which eventually spread across all of Microsoft. The Standard Form of Hungarian Notation was defined as having three parts - Tag, Base Name, and Qualifier.

Formalization produced two primary flavors of Hungarian - Apps Hungarian and Systems Hungarian. These differed primarily in the way the Tag is used. Apps Hungarian utilizes the storage-type prefix (strName) for the Tag. Systems Hungarian was more use driven, with two common approaches - colorBackground (system use) or m_Background (scope). M stands for "member", commonly used to indicate a private variable or backing variable for a property. The m_ was so common is code that many dropped the m, and simply used _. Qualifiers were used to extend the semantics. Commonly used qualifiers were things like min, max, first, and last - intRangeMin and intRangeMax. A alternative form used the older "meta-type qualifier" approach - intBackgroundColor.

Visual Basic

As languages advanced, most languages supported declaring type with the variable. IDE's provided better visibility into types (Intellisense), and made Apps Hungarian less useful. But the popularity of Visual Basic increased the Hungarian overall. The structure of Visual Basic often links a "visual" form file with a backing code file. The code file commonly responded to events raised by "controls" in form. It became a standard approach to use Hungarian to maintain linkage between these files by prefixing a variable with the control type. Most of us are familiar with btn_Accept or txt_FirstName. Qualifiers became associated with events. The event handler for a button click event was named btn_Accept_OnClick(). The BaseName "Accept" and the Tag "btn, and the Qualifier "Click" (OnClick).

The GUI approach to building forms in VB (and ASP) was backed by code generators. To maintain semantic naming and avoid potential name collisions, it was common for the generators to append the control type to the control name. A textbox for First Name would receive the name FirstNameTextBox, to avoid coliding with a user variable txt_FirstName. A button might be AcceptButton. This form is Systems Hungarian in reverse, and was named "Reverse Hungarian".

So, What is Hungarian?

We can see that Hungarian Notation is not just the one form "strName", - typeVariable, or even [Tag][BaseName]. Any name that that uses variations of [Tag][BaseName][Qualifier], including reversed [Qualifier][BaseName][Tag] is a Hungarian name. And, while a common form (Apps Hungarian) uses storage types (e.g., int) for Tags, others (System Hungarian) don't. An online dictionary gives this definition for Hungarian Notation:

"Hungarian notation is an identifier naming convention in computer programming, in which the name of a variable or function indicates its intention or kind, and in some dialects its type."

Contrast this definition to what many people might say when asked to define Hungarian Notation. Maybe -

"Hungarian Notation is prefixing a variable name with an abbreviation for its datatype."

The first defines Hungarian Notation, the second only one flavor of Apps Hungarian that uses Tag and BaseName, and omits Qualifiers.

Hungarian Today

Fast forward to today. Do you do DI and name your local injected variables something like "_context"? Hungarian. IInterface, iInterface, or i_Interface? Hungarian. Do you use ASP.Net MVC? The entire MVC convention-based system is Hungarian. Are your controllers named SomethingController? Hungarian (Reverse). Async? If you append your async methods with Async, you are using a Hungarian Qualifier. Apps Hungarian (e.g., strName) provides little value in modern programming, and I think its use should be discouraged. But if we eliminate the use of all Hungarian, what are the options? Hungarian has reigned as our naming convention since the 80's, and will continue to be our standard for the foreseeable future. Hungarian is Dead, Long Live Hungarian!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

RandyBuchholz
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionMessage Removed Pin
14-Aug-17 21:30
memberMember 250475814-Aug-17 21:30 
QuestionApps vs Systems Pin
Stefan_Lang24-May-17 0:04
memberStefan_Lang24-May-17 0:04 
QuestionMissing the Point Pin
RandyBuchholz17-May-17 6:28
memberRandyBuchholz17-May-17 6:28 
AnswerRe: Missing the Point Pin
David O'Neil17-May-17 7:21
professionalDavid O'Neil17-May-17 7:21 
GeneralRe: Missing the Point Pin
RandyBuchholz17-May-17 10:51
memberRandyBuchholz17-May-17 10:51 
AnswerRe: Missing the Point Pin
David O'Neil17-May-17 9:59
professionalDavid O'Neil17-May-17 9:59 
GeneralRe: Missing the Point Pin
RandyBuchholz17-May-17 10:54
memberRandyBuchholz17-May-17 10:54 
QuestionCan't be stamped out soon enough... Pin
Member 824164215-May-17 7:10
memberMember 824164215-May-17 7:10 
AnswerRe: Can't be stamped out soon enough... Pin
CDP180216-May-17 0:42
memberCDP180216-May-17 0:42 
QuestionNeeds more clarity Pin
bfrthekid9913-May-17 16:39
memberbfrthekid9913-May-17 16:39 
GeneralPersonal taste Pin
ameliaamelia10-May-17 11:30
memberameliaamelia10-May-17 11:30 
QuestionGot my 5 Pin
Flaviu29-May-17 23:31
memberFlaviu29-May-17 23:31 
GeneralMy vote of 5 Pin
David A. Gray9-May-17 20:46
memberDavid A. Gray9-May-17 20:46 
QuestionMy vote of 1 Pin
David O'Neil9-May-17 17:10
professionalDavid O'Neil9-May-17 17:10 
AnswerRe: My vote of 1 Pin
hooodaticus17-May-17 6:57
memberhooodaticus17-May-17 6:57 
Questionit will be greater Pin
Southmountain9-May-17 15:23
memberSouthmountain9-May-17 15:23 
GeneralConsider including a link to this... Pin
PIEBALDconsult9-May-17 13:48
protectorPIEBALDconsult9-May-17 13:48 
QuestionMy Vote of Two Pin
Rick York9-May-17 13:39
memberRick York9-May-17 13:39 
AnswerRe: My Vote of Two Pin
hooodaticus10-May-17 9:47
memberhooodaticus10-May-17 9:47 
GeneralRe: My Vote of Two Pin
Rick York10-May-17 15:21
memberRick York10-May-17 15:21 
GeneralRe: My Vote of Two Pin
hooodaticus17-May-17 6:55
memberhooodaticus17-May-17 6:55 
AnswerRe: My Vote of Two Pin
CDP180216-May-17 3:56
memberCDP180216-May-17 3:56 
SuggestionNot quite what Hungarian Notation is and isn't. Pin
wvdhouten9-May-17 11:39
memberwvdhouten9-May-17 11:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171019.1 | Last Updated 9 May 2017
Article Copyright 2017 by RandyBuchholz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid