Thursday, June 26, 2008

Hungarian Notation

In Microsoft, a man named Charles Simonyi created specification for writing Microsoft code. All Microsoft APIs, interfaces, technical articles, and so on use these conventions.
The specification is generally referred to as Hungarian notation

Hungarian notation consists of a number of conventions relating to naming:
• Variables
• Functions
• Types and constants
• Classes
• Parameters

Following figure shows all the prefix codes used in Hungarian notation. These codes are used to prefix variable names in most cases, along with other conventions depending on what is being named.



Variable Naming

With Hungarian notation, variables are prefixed by the codes mentioned in above figure. In addition, if a variable name is made up of one or more subnames, each subname is capitalized.


Basically, you begin the variable with g_, or sometimes just plain g.


Function Naming

Functions are named in the same way variables are, but without the prefixes. In other words, just capitalize all the first letters of subnames. Here are some examples:

int PlotPixel(int ix, int iy, int ic);
void *MemScan(char *szString);


Also, underscores are illegal. For example, the following wouldn’t be a valid
Hungarian-compliant function name:

int Get_Pixel(int ix, int iy);

Type and Constant Naming

All types and constants are in uppercase, but you’re allowed to use underscores in the names. For example:



Class Naming

All C++ classes must be prefixed by a capital C, and the first letter of each subname of the class name must be capitalized. Here is an example:



Parameter Naming

Parameters to functions follow the same naming conventions that normal variables do.
However, this is not a necessity. For example, you might see a function definition that looks like this:

UCHAR GetPixel(int x, int y);

In this case, the more Hungarian prototype would be

UCHAR GetPixel(int ix, int iy);

Also you might not even see the variable names, but just the types, as in this example:

UCHAR GetPixel(int, int);

Of course, this would only be used for the prototype, and the real function declaration must have variable names to bind to.


Programming Ring - New Post
Programming Ring - Old Post

No comments: