Home   Info   DevZone   Wiki  
UsersWeb  |  MainWeb  |  InfoWeb  |  DevZoneWeb  |  SupportWeb
HungarianNotation ] [ not logged in ] [ Web: Imported ] goto:  options
[ get info on or edit ] login or new user ] [ list of topics, hubs & nodes, or recent changes ]

HungarianNotation is a function and variable naming standard proposed by Charles Simonyi (I think this is where the "Hungarian" comes from, but I don't know.) at Microsoft.

It's used most extensively for C programming, although I have seen it in C++, Delphi and a host of other languages. The goal is to include as much information in the name as possible.

See: http://www.strangecreations.com//library/c/naming.txt for the text of Simonyi's original monograph. -- BillRehm


Ok. Here's a quote from a document at NetscapeCommunications.

This technique makes programming easier, because it gives you a simple mental machine for naming the things in your code.

While he was working at Xerox PARC, Charles Simonyi invented a technique that solves an old and tiresome programming problem: "What am I going to name this variable?" Simonyi realized that there are two important aspects to think about when writing code: what a thing is, and what a thing means. To write good programs in strongly-typed languages, you really need to keep both of these things in mind. The "traditional" naming technique uses names that emphasize what a thing means, requiring you to memorize its declaration to remember what that thing is. While good in its intention of making code "readable", this traditional kind of naming often results in two things:

Why is this technique called "Programming in Hungarian"? There are two reasons. First, Charles Simonyi is himself Hungarian. Second, the names resulting from the technique don't look like English, although they often have English words embedded in them. But because they carry a lot of information in a simple and regular structure, Hungarian names are actually more readable than English in the context of a computer program.

Hungarian names have two parts. The prefix contains type information specifying what this thing is, while the suffix contains descriptive information specifying what it means. The prefix is composed of standard elements that are the same for any program you write in a given language. The suffix is composed of English words that tend to be application-specific.

The prefix elements used here are not the only ones possible, but they have some particularly nice properties. First, they are highly mnemonic, so it's easy to remember what they mean. Second, they all consist of a single letter. This becomes important when multiple prefix elements are composed in the same name. A prefix can convey a lot of information in a few characters, without possibility of confusion about how those characters are associated.

__Good Hungarian Prefixes for Java__

	 a - array					n - number of
	 b - byte					 o - object
	 c - character			  p - parameter
	 d - double				  q - 
	 e - enumeration			r - 
	 f - float					s - short
	 g -							t - true-or-false
	 h -							u - 
	 i - int					  v - 
	 j - junk					 w - 
	 k -							x - exception
	 l - long					 y - 
	 m -							z - size

Prefixes are built with lower-case letters only . The prefixes for most variables in Java are o, i, or t. When more than one prefix element is needed to describe something, the elements compose to the left. Thus, the names of most parameters in Java methods start with po or pi or pt. The name of a variable that refers to an array of floats starts with af.

While prefixes involve standard abbreviations and compose to the left, suffixes involve suitable English words and compose to the right. Not all names need to have a suffix. Avoid abbreviations in suffixes, unless they are extremely common; you're doing no one a favor if your names require much imagination to be understood. The first letter of each suffix word starts with a capital letter. The first word of a suffix is usually a relevant class or interface name. If any other suffix words are necessary, they are usually adjectives that qualify the first word. For example, if you are sorting objects into Vectors by color, you might have variables called oVectorRed, oVectorGreen, and oVectorBlue.

By building the prefix to the left and the suffix to the right, the structure of the names makes it easy to see how things are related to each other. You'll find that the variables in your expressions tend to have a lot of similarity in their names. When they don't, it's usually a hint that you've done something wrong. You'll catch a lot of errors yourself, without having to wait to compile or test to learn about them. Also, names with this built-from-the-middle-out structure are particularly easy to manipulate using the search and replace features of programming editors.

You'll soon notice several benefits from this technique. First, you'll spend less time thinking about what to call things. Second, you'll find that your programming tools can do more to help you. Third, you'll find it easier to read code written by others, if they are also using Hungarian naming, because they usually choose the same names you would have if you had written that code. If the JDK was written this way, so that methods had Hungarian names, too, many automated programming tools would be much easier to build.

-- MarkusPeter (in the hope NetscapeCommunications will not sue him now for quoting this)




Content of these pages are owned and copyrighted by the poster.
Hosted by: