Click to See Complete Forum and Search --> : Advice on using Hungarian notation
C++ Newbie
February 2nd, 2003, 08:10 PM
Hello, my coworker and I are new to C++ and the Hungarian notation. We both agree on its usefulness for labeling members of classes and are labeling the data type as well:
e.g.,
m_nNumberOfHoops = 12;
m_strFileName = "C:\logs\june010102.lgh"
The place where we don't agree is whether we should use Hungarian notation within local functions. I think it muddies the waters but he likes it because of consistency. We would like some advice on C++ veterans on what you do and how/where you choose to use Hungarian conventions.
Thanks!
PaulWendt
February 2nd, 2003, 08:45 PM
All that REALLY matters is that you guys pick a convention and
stick with it. When it comes to a lot of things in programming,
anything that's not directly related to performance is pretty much
open to interpretation ... and EVERYONE feels that they are right
so you get a lot of "religious" type discussions :)
Having said that, if I were going to use hungarian notation, I'd
use it everywhere. I think that consistency is very important ...
but there ARE, of course exceptions for every rule. When I used
hungarian notation a lot, I never used it on loop variables. I
typically use i, j, or k for loop variables; it's a fairly well-known
convention.
Nowadays, I don't use that much hungarian notation. I just like
the descriptive names and try not to worry about type all that
much. The only thing I REALLY try to enforce is that I always
name class member variables with the m_ prefix ... and I always
name my pointer variables with a p prefix. I like my functions
short and there just doesn't seem to be a whole lot of purpose
for hungarian notation IN MY OPINION. As your programs get
larger and larger, you find yourself using the built-in types less
and less ... and there's really no "convention" for user-defined
types ... or the "convention" makes them all look the same
anyway, so I just rely upon creative names.
Your mileage may vary :)
--Paul
KingTermite
February 2nd, 2003, 09:09 PM
I'll take the vote of consistency. It's not so much that it "matters". It's more of the fact that it can slow down your "thoght process" by going back and forth from one way to the other. Just pick one and stick with it.
Me personally...I hated hungarian at first when I was forced to use it as a coding standard at work. Now, I've gotten so used to it that in my own personal coding, I've come to use it by repetition.
Gabriel Fleseriu
February 3rd, 2003, 01:44 AM
I agree with Paul: it is important to be consistent.
I personally don't like the hungarian notation because it makes the identifiers hard to pronounce, you have more to type and it can be a plain lie in some cases (if you change the type of a variable, you need to change the variable name, too).
I accept the m_ prefix in MFC but don't use it for my own classes that are not MFC related. I also sometimes use a "p" for pointers, but that's all.
If you want to know the oppinion of a real C++ guru, read this article (http://www.cuj.com/experts/1911/hyslop.htm) .
proxima centaur
February 3rd, 2003, 12:55 PM
This is the kind of issue that can lead to a heated debate. For reference, take this thread:
http://www.codeguru.com/forum/showthread.php?s=&threadid=211546&highlight=hungarian+notation
The only thing to keep in mind is that whatever decision you will take, some people will disagree with it :).
So do what you want !
:D
HeartBreakKid
February 3rd, 2003, 01:28 PM
my eyes are open. Interesting reading.
Gabriel Fleseriu
February 3rd, 2003, 02:02 PM
Originally posted by proxima centaur
This is the kind of issue that can lead to a heated debate. For reference, take this thread:
http://www.codeguru.com/forum/showthread.php?s=&threadid=211546&highlight=hungarian+notation
The only thing to keep in mind is that whatever decision you will take, some people will disagree with it :).
So do what you want !
:D
Yup, I agree totally: we are touching a very religious issue here :D
Graham
February 3rd, 2003, 02:58 PM
Well, I'm going to come down firmly against Hungarian notation. I don't like it - it's ugly and it can be misleading. As the Hyslop article asks: how do you wartify a template parameter type? What about derived classes? Or, to put it more simply, what rules do you apply to create a wart from an arbitrary type name? If I have class Bird, what's its wart? And if I then derive FlightedBird and FlightlessBird, what are their warts? What's the wart for class Albatross, derived from FlightedBird? Is it important to know?
It's much more important, IMHO, to name variables accurately by their purpose rather than their type (which may actually be irrelevant).
gtrdhyft
February 3rd, 2003, 05:44 PM
It's much more important, IMHO, to name variables accurately by their purpose rather than their type (which may actually be irrelevant).
It is indeed something pretty religious when it comes to coding style. Actually the whole art of computer programming is religious. Some one from another planet may program in a completely different paradym but one that could be just as effective.
But it doesn't mean that it is not discussable. The advantage/disadvantage of any coding style can be quantified by the likelihood of one group of average programmers generating bugs due to mis-read or mis-understanding of code written in that particular coding style.
In that sense, since Hungarian has a larger audience than any other naming convensions, it is a convension that can be more accurately understood by more people. Code written in Hungarian has a better chance to be understand by other people accurately, without mis-interpretation.
I don't know how come variable types are irrelevant. They are certainly very relevant to the code. So I don't think there is any thing wrong with integration the variable type as part of a variable name.
Long variable name is not necessarily a bad thing for having to type in longer names. After all, the time programmers spend on typing is probably less than 1% of total development time. Much more time is spend on trying to read and understand your colleague's code, and/or try to debug a bug that is generated in the first place because of some mis-understanding. So, typing long names is never an issue, making the code readable is.
mwilliamson
February 3rd, 2003, 06:29 PM
I like to use my own version of the hungarian notation everywhere. For classes, I generally don't use the hungarian notation, but still m_ for class members (which is helpful even you don't want to include the type, because it tells you where to look for the type :)). For mfc classes like CComboBox or CButton I use c and btn. Most of my classes don't have a good prefix, so I don't use one. For templates, I like to use what I called the type name parameter for the "wart" (what a stupid name!). eg.
template<typename T>
T SomeFunction(T tVariable)
{
std::string sString;
return tVariable + sString;
}
Being a lazy bugger, I use n for integers (shorts, longs, whatever) and f for floating points (floats, doubles...). I think it helps, especially for numbers, because you don't know if you size or count is a real or integer, it can screw up your math.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.