CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2002
    Posts
    71

    Advice on using Hungarian notation

    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!

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  3. #3
    Join Date
    Jul 2002
    Location
    Seattle Area, WA
    Posts
    241
    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.

  4. #4
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    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 .
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  5. #5
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374
    This is the kind of issue that can lead to a heated debate. For reference, take this thread:

    http://www.codeguru.com/forum/showth...arian+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 !

    Martin Breton
    3D vision software developer and system integrator.

  6. #6
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484

    Thank you....

    my eyes are open. Interesting reading.

  7. #7
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    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/showth...arian+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 !

    Yup, I agree totally: we are touching a very religious issue here
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  9. #9
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    2
    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.

  10. #10
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured