Skip to main content
GameDev.net gamedev.net
🔒 Locked

Hungarian notation

Started by Caesar Nov 25, 2001 at 9:27 AM 13 replies 1.8k views
Original Post
Caesar
Caesar
Hi everyone, I''d like to know if there''s someone out there who could tell me something more about Hungarian notation. I hope it is what I mean ie. int iNumber; LPSTR szString; etc. is there some description somewhere? Do you use it and if, how often. If not, what style do you prefer.
Oluseyi
Oluseyi
Google could tell you tons, and could also point you to others who could tell you more.
Shannon Barber
Shannon Barber
I don''t know all of Hungarian notation, but I picked up on some of it reading over various Win32 C sources.

The most important part of a naming convention is to indicate the scope of the variable. There''s some disagreement on whether or not HN (aka waRTs) improve the "readablity" of the code.

This is the scheme that I use:

Prefixes:
g_ global
m_ member property
ms_ static member
&ltno prefix> local

Postfixes:
_ function or method parameter
_&ltunit> to indicate what units a measurement is in



  
const float ms2s = 1.0f / 1000.0f;
fDistance_m = m_fVelocity_mps * (dwElapsedTime_ms * ms2s);


Magmai Kai Holmlor
- Not For Rent
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
CGameProgrammer
CGameProgrammer
My typing style changes all the time, but currently I use scope prefixes and that''s it. Magmai lists most of them, here''s one more:

s_ - static (usually can be either local or global)

True Hungarian Notation involves one or two letters identifying the type of the variable. Most people really hate this, but it really makes code much easier to understand for people who didn''t write it. Tutorials should use this notation, in my opinion. Microsoft uses it because its code is worked on by many people, and this makes it easier for that to happen, so they can understand each other''s code.

Here are some type prefixes:

i - Integer. Signed, either 16-bit or 32-bit

l - long. That''s a lowercase L

c - char

s - array of char. In other words, a string NOT terminated by zero
Example: char sRGB [3];

sz - string terminated by a zero
Example: char* szText = "Hi, world!";

a - array, followed by type. Sometimes ''r''
Example: int aiArrayOfInt [5];

f - float

d - double

u - unsigned, followed by type
Example: unsigned long ulWidth;

b - byte, same as unsigned char

w - word, same as unsigned short

dw - doubleword, same as unsigned long

p - pointer. Usually not followed by anything
Example: float* pAges;
usually not: float* pfAges;

Scope stuff comes before type stuff.
Example: g_szName
not: szg_Name;

~CGameProgrammer( );

~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Caesar
Caesar
ok, thanks a lot... I think that after that I''ll stay with my current style
Oluseyi
Oluseyi
Might I add prefix for STL containers/adapters?

vec - vector<>
str - string<>
lst - list<>
map - map<>
...

I hardly ever use plain arrays or C-style strins anymore in my own code. It''s only in those regrettable places where I have to interact with existing interfaces... speaking of which, does anyone know how to pass the contents of an STL string to Speak() in the MS Speech API?
AdmiralBinary
AdmiralBinary
Doesn''t string::c_str() work? Or does it want a non-const string? Maybe const_cast would work. I dunno...

---------------

I finally got it all together...
...and then forgot where I put it.
Oluseyi
Oluseyi
quote:
Original post by AdmiralBinary
Doesn''t string::c_str() work? Or does it want a non-const string? Maybe const_cast would work. I dunno...

For some bizarre reason, Speak() takes a const WCHAR * for the text. When specifying the text hardcoded, one must use the L macro:
pVoice->Speak(L"Hello", ...); 

I haven''t found any conversions from any of the C-style strings to WCHAR * (which is actually an unsigned int *).
Oluseyi
Oluseyi
I''m currently looking into AllocSysString().
AdmiralBinary
AdmiralBinary
LOL, I s''pose this wouldn''t be any good to you?
  
WCHAR * WChar(char * str)
{
WCHAR * ret = new WCHAR[strlen(str)];
for (UINT i = 0; i < strlen(str); i++)
{
ret[i] = str[i];
}
return ret;
}


---------------

I finally got it all together...
...and then forgot where I put it.
AdmiralBinary
AdmiralBinary
Ooooooh, my first double-post, sorry...

---------------

I finally got it all together...
...and then forgot where I put it.
Lowas
Lowas
True Hungarian notation blows!
C++, C++, C++
S1CA
S1CA
WCHAR is a "Wide Character". This is for UNICODE characters, these are used so that foreign languages with more than 256 characters/glyphs/letters which are possible with ANSI can be represented - a common example is one form of Japanese which has over 3000 characters. Using 16 bits per character (WCHARs are 2bytes/16bits each) allows 65536 possible characters. For more information on UNICODE look at: http://www.unicode.org/

Windows NT/2000/XP use UNICODE internally so many Windows APIs which were originally written for NT only take UNICODE strings.

There is a third form of character in Windows called Multi-Byte-Character-Set (MBCS), in this is like a hybrid between ANSI and UINCODE where western characters (A-Z etc) are represented with just a single byte. For characters which won''t fit into the 0-255 range, a special code is used to mark the introduction of an extended code so that individual characters could take different amounts of space between 1 and 3 bytes each.

Unicode is the easiest and standard way to represent strings in languages with lots of characters.

Some Windows calls to look up for conversion of strings:

MultiByteToWideChar()
IsTextUnicode()




--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
Oluseyi
Oluseyi
Muchos gracias!
Oluseyi
Oluseyi
S1CA, you''re the man! it''s working beautifully now. Now I need to focus on annotations for a more life-like representation.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.