Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hungarian Notation 3

Status
Not open for further replies.

Skute

Programmer
Jul 21, 2003
272
GB
When naming variables there are many different methods you can use. By far the best and most wide-spread method is called "Hungarian Notation". This involves prefixing names of variables with a short character(s) representing the datatype of the variable. Coders all over the world use this type of notation, so if possible, it is best to use it because it will be alot easier for anyone else picking your code to understand.

Here are some examples:


Numbers
// This variable is an integer, and is involved in some counting procedure
int iCount = 0;
// A floating point number for measuring height
float fHeight = 0.0f;

So when you come across these 2 variables in your code you should be able to quickly and easily identify what type they are and what purpose they have.

With numbers types it is also common for people to use the "n" prefix, which simply means number. So the previous 2 examples would become:

int nCount = 0;
float nHeight = 0.0f;

But i dont like that way as much because you cant quickly tell which type the variable is.


Strings

char szName[256] = "Skute";
char szName2[] = "Skute2";

The "sz" prefixing the name of the variable means "string terminated with a zero (NULL)". So for example, the variable szName2 can be read as:

szName2[0] = 'S'
szName2[1] = 'k'
szName2[2] = 'u'
szName2[3] = 't'
szName2[4] = 'e'
szName2[5] = '\0' (NULL)

Here are some other ways of writing string definitions:

char* pszCountry = "England";

The "psz" here means "pointer to a string terminated with zero (NULL)".

string strDog = "Spike";
CString strCat = "Tom";

Here the "str" means string type or string class.


Member and global variables

When variables belong to a class they can be named slightly different so they are easier to identify then local variables. For example:

int m_iPort = 3306;

The "m_i" prefix means "class member of type integer"

Or similarly:

char* g_pszConnection = "Server=localhost";

Can be read as "global pointer to a string terminating with zero (NULL)".




I hope that was helpful to someone out there! If you need any other examples of variable naming i can easily tag some onto the end.



Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
Great, I really like your spirit

Ion Filipski
1c.bmp
 
very good for newbies [bigsmile]
 
Let me guess Skute, you had to edit someone's code that used crazy variables lol.

Just kidding, its always nice to see comments on something insead of all questions, esp. for those not particularly looking for anything except a general tip they migh pick up.

-Bones
 
yeah the most annoying thing is looking through someones code and they have used variables like this:


*p = a;
bf = j;

etc etc :p

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
Skute,

Please make FAQs out of these instead of posting in the question forum.



/Srikanth

What happens if you’re scared half to death twice?
 
Having such a convention does not prevent others to use variable whose names are obscures :

Code:
int i_j;
char* sz_stuff;

Nothing can replace good, clear, understandable variable names, regardless of the type.

Furthermore, an apparently well-know drawback of the hungarian notation is the problem of maintaining coherence between variable names and the actual type.
If you have :

int iHeight;

and later you realized that a float is better :

float fHeight;

This leads to change "iHeight" to "fHeight" everywhere in your code.

--
Globos
 
yeah thats why as ive said some people tend to use

int nHeight = 195;

or

float nHeight = 195.394;

;)

n = Number

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
Ive now submitted it as an FAQ too:

"How should i name my variables?"

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
however programmers usualy subestimates a good programming style, I consider that post very helpful. I have seen what only professionals have a good coding style and only beginners hate good programming style. So, good coding style is an important step in becoming a professional.
Using bad coding style in order to become a proffessional is something like trying to become a champion of KungFu without learning to walk first.

Ion Filipski
1c.bmp
 
>Skute :
>yeah thats why as ive said some people tend to use
>
>int nHeight = 195;
>
>or
>
>float nHeight = 195.394;
>
>;)
>
>n = Number

Using n=number as a prefix for a variable named 'Height', I think this is really redundant. It is clearly obvious that 'Height' is a number(int or float) or a class that models a numeric.

On the other side, I will tend to think that one should prefix the variable name with a reminder denoting the most abstract type possible, like n=number for the case of height, or the type of a base, deferred class rather than an concrete class.

Today, the only two prefix I use, is '_' for class' attributes.
And prefix 'i' for iterators, followed by the type of the objects hold by the traversed container :
Code:
  list<Material> materials;
  ...
  list<Material>::iterator imaterial = materials.begin ();
I use the convention 'type of the object' + suffix 's' for variable that are containers, like 'materials' for list<Material>.

By the way what the Hungarian notation advise in that case? limat_materials?

--
Globos
 
>globos
when you put a letter in the code to make it more readable the program performance does not decrease and size does not increase. About coding conventions you can find a lot of materials in the internet, so search the internet for more information.

Ion Filipski
1c.bmp
 
I've never said that convention namings decrease performance at execution, but I would not say that it makes the code more readable, and you can find also a lot of materials in the internet arguing that Hungarian style is a bad naming convention, so search the internet for more information ;-)


--
Globos
 
I didn't say what Hungarian coding style is the best. I have my style, which does not correspond to it. But Hungarian style is much better than no style. So, search the internet for more information [LOL]

Ion Filipski
1c.bmp
 
>> By the way what the Hungarian notation advise in that case? limat_materials?


nope ;)

When i use iterators i declare them as follows:

struct tagClient
{
...
};

vector < tagClient > vClients;

vector::iterator < tagClient > itClient;

Still as clear :)

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
OK, I give up, I stop this infinite topic here :)
But, I must say that Skute introduces the Hungarian notation as the 'best' one, and most of the replies seem to agrees with this(especially you :)). I would like to know why it can be considered as the best naming convention, maybe because it is wide-spread?

Hungarian notation was made originally for C, from a Microsoft employee. It is the notation used in the Windows C API, when one see how this API is designed(cryptic function names(the Ex), LPSTR, etc.) I feel really wary of that Hungarian notation.

I've read somewhere the parallel between the Hungarian notation and the Polish notation mathematical notation, which is pretty hard to read for humans, strange isn't it? In fact the creator was Hungarian.

Naming convention is an interesting subject to debate on, maybe ion you can share some of your elements of your own style here, without telling &quot;search on the internet for more information&quot; please :)

--
Globos
 
I also give up because I wouldn't share my coding style. It is not a secret, but I don't consider it is necessary. If you are so curious you can contact me directly, some of my contact information is in my details.

Ion Filipski
1c.bmp
 
What maybe interesting to add to your FAQ are a few general rules to define a good reminder for variables typed with user-defined classes (other than number and strings).

--
Globos
 
Why it is bad

I agree with IonFilipski that using styles are good (or even necessary for any professional developer). Unfortunately makes Hungarian Notation a programmer's life harder rather than easier. And we all want life to be easy - right?

Why is it bad? Well, simply because when you incorporate the variable's type in its name you make your code harder to maintain. You would have to update all occurances of that variable if you decide to change the type (for example from pointer to ref, char* to CString etc). This can be quite a lot of work, so either you

1) live with a type that you feel isn't quite appropriate (ie you do not improve the code, even though you've figured out an improvement that could make sense), or
2) change the type and live with inconsistent names (and then the notation actually becomes erroneous and misleading), or
3) actually makes the necessary changes. Note that the work you put into it will not improve the quality of the code (in terms of lesser bugs, more flexible, bugdetecting, functionality) a single bit. Ie some hour later your still where you started, only with a slightly different looking code. Ask your project manager (or the ony who's paying your salary) what he thinks of that kind of work.
------
Why you dont need it

We dont use globals - right? Ie no need to browse the files about the variable's definition.

Variables are thus either members or locals.

Getting info on members is provided by the class' interface (and automatic in Visual Studio). Since class members is a private thingie it is only interesing for the implementation thats using them. Getting the .h for a corresponding .cpp is trivial.

Only locals left. Now, if one follows the paradigm of keeping methods/functions single-purpose and small you'd probably see the class definition as you work with the function (or,say, 1-2 screens up).
-----
Conclusion
Hungarian Notation provides un-flexibility to your code that you really don't need.

Cheers to Skute for posting info on what H.N. is, however I think it should be presented as a bad-practice example...

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top