Welcome, Guest! Login | Register

Hungarian Notation [Print this Article]
Posted by: rkzad
Date posted: Jun 09 2003
User Rating: 5 out of 5.0
Number of views: 6360
Number of comments: 1
Description: And why you should use it
This tutorial is inspired by MutantSquirrel and is about Hungarian notation.

Hungarian notation is a variable-naming convention. Using Hungarian notation, a programmer could take a look at a piece of your code using a variable named "szHawaii" and immediately recognize it as a string without searching all over the place for the definition of the variable. This is very helpful especially when more than one programmer has to deal with the same code. Even if you're the only one working on it, it will require less thinking on your part when you're dealing with your own code. The general principle is you add a short prefix in lower-case infront of your variable name to help show what data type the variable is.

Here is a list of the Hungarian notation conventions as given by Microsoft (Hungarian notation was apocryphally named in honour of Microsoft programmer, Charles Simonyi):
 CODE  
Prefix   DataType
b        Boolean
by       byte or unsigned char
c        Char
cx / cy  short used as size
dw       DWORD, double word or unsigned long
fn       Function
h        Handle
i        int (integer)
l        Long
n        short int
p        a pointer variable containing the address of a variable
s        string
sz       ASCIIZ null-terminated string
w        WORD unsigned int
x, y     short used as coordinates

Also when you're declaring variables, you will want to keep the scope in mind (local, member, global) and whether it is static or not. To name your variables for this, right the given prefix needed, followed by an underscore, followed by one of the prefixes above depending on the data type (ex m_iNumber).
 CODE  
m_       member variable
g_       global variable
s_       static variable
local variables don't need an extra prefix
You can also combine static and member/global variables by butting the letters together, i.e. ms_iNumber.

A couple other conventions come up when declaring classes:
 CODE  
C        regular class (ex CBob)
I        interface (ex IDirect3D9 - a class with no member variables and no implementation)

Conventions will range from programmer to programmer and from code to code, but all are generally similar. There is no right or wrong way to do this, but you should just make sure that your code is easily analyzable. This is a really good thing to practice, and it will see how it can end up being helpful, especially when coming back to code you haven't touched in a while, and when your code is getting rather large.

Reference:
Variable Names and Hungarian Notation
http://msdn.microsoft.com/library/default....iannotation.asp

Rate This Article
This article is currently rated: 5 out of 5.0 (3 Votes)

You have to register to rate this article.
User Comments Showing comments 1-1

Posted By: [ICR] on May 15 2005 at 16:18:47
What if you want to change what type a variable is? You would have to find every single referance in the code and change it. Some more recent IDE's (well, I have noticed the more recent Borland Delphi ones) allow you to change variable names all at once, which would solve it I suppose.


You must register to post a comment. If you have already registered, you must login.

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 05 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005


Latest Comments
knock knock
General | News
By: omega | Dec 22 2016
 
knock knock
General | News
By: MIFUNE | Oct 10 2015
 
New HL HUD Message System
Half-Life | Coding | Shared Tutorials
By: chbrules | Dec 31 2011
 
knock knock
General | News
By: Whistler | Nov 05 2011
 
Particle Engine tutorial part 4
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 18 2010
 
Particle Engine tutorial part 2
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Particle Engine tutorial part 3
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Game Movement Series #2: Analog Jumping and Floating
Half-Life 2 | Coding | Shared Tutorials
By: mars3554 | Oct 26 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Deadpool | Aug 02 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Persuter | Aug 02 2009
 

Site Info
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (9 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!