Thread: hungarian notation

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    hungarian notation

    Is hungarian notation still used a lot in modern code or is it slowly weeded out?

    I thought it was not so useful anymore in modern typed languages and with all the OO and generic coding paradigms these days but it seems some programmers still cling to it.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I still see it quite frequently in the project I'm working on, though we try to avoid it. We do use a modern OO language (ActionScript) but we do a lot of work with a Java back-end, and so types aren't always what they should be. Purposes can also be obfuscated - so we'll include that information in the names as we go. There's probably a better solution - but you have to build on what you're given sometimes.

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Quote Originally Posted by KIBO
    hungarian notation
    *vomits*
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is hungarian notation still used a lot in modern code or is it slowly weeded out?
    Real Hungarian or the misinterpreted and bastardized Hungarian that was a stupid idea from day one?
    My best code is written with the delete key.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Quote Originally Posted by KIBO View Post
    Is hungarian notation still used a lot in modern code or is it slowly weeded out?

    I thought it was not so useful anymore in modern typed languages and with all the OO and generic coding paradigms these days but it seems some programmers still cling to it.
    Prepare to meet a lot of it. Not in new code, but in old code. Working code doesn't get replaced overnight just because someone invented a new shiny $buzzword technique. The reason for using hungarian notation is mostly gone and it will slowly fade out. But slowly. As in decades.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Prelude View Post
    >Is hungarian notation still used a lot in modern code or is it slowly weeded out?
    Real Hungarian or the misinterpreted and bastardized Hungarian that was a stupid idea from day one?
    Can you give examples of both?

    I use only a couple notations regularly like:
    pVariable - for pointers
    m_Variable - for member variables

    Other times I might use others to distinguish variables with similar names, but I don't use the ugly stuff like: lpszStr...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by cpjust
    Can you give examples of both?
    She's probably talking about the difference between Apps Hungarian and Systems Hungarian. In my opinion, Apps Hungarian is mainly just a uniform way of abbreviating variable names, whereas Systems Hungarian is mainly a way to insist that the type system is useless.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by KIBO View Post
    Is hungarian notation still used a lot in modern code or is it slowly weeded out?

    I thought it was not so useful anymore in modern typed languages and with all the OO and generic coding paradigms these days but it seems some programmers still cling to it.
    I still use it, but not religiously. I ge3neralyl use it when I'm modifying someones coede that already used it, like some of the examples on MSDN, or if the type is vitlly important to the code, such as large memory realignment operations. It's definatley useful tyo learn it and learn to use it so when you see it in code it doesnt break your rythym.

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I never use it and never used it. Meaningful names can actually save you a lot of time.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    She's probably talking about the difference between Apps Hungarian and Systems Hungarian
    I often hear people refer to
    Code:
    excessively_long_overly_descriptive_variable_names
    as "hungarian notation". Maybe she means that.

  11. #11
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I like descriptive and concise names, although it's often harder to pull out of the top of your had than hungarian notation.

  12. #12
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Hungarian notation is like lpwszStr (which is a long pointer to a wide zero-terminated string) or lphwndHandle (of type long pointer to window handle). Certainly, I'm exaggerating a bit. Nobody would propably need a lphwndHandle.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by indigo0086 View Post
    I like descriptive and concise names, although it's often harder to pull out of the top of your had than hungarian notation.
    I don't think Hungarian Notation and descriptive names are mutually exclusive. Hungarian Notation is just a prefix to a variable name. So you could have something like:
    lpszFirstName
    which is perfectly descriptive, albeit ugly to look at.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    I haven't seen a sane programmer use either Apps or Systems Hungarian. While Systems Hungarian is plain ridiculous, one might argue that Apps Hungarian helps [morons] choosing consistent variable names; but in my opinion, there are far better alternatives, the most prominent one being common sense.

    Consider the following two code snippets to naively determine the length of a string:

    Code:
    culCount = 0;
    
    for(ichIndex=0; rgchText[ichIndex] != '\0'; ichIndex++) culCount++;
    return culCount;
    And here's the second one:

    Code:
    for(i=0; a[i] != '\0'; i++);
    return i;
    The second one is much shorter and not the least bit difficult to understand. Note that you can easily guess the types of all variables and furthermore that the variable i serves two different purposes: it's an array index and a counter. This isn't even expressible in Apps Hungarian.


    Apart from readability, there are some more disadvantages when using Hungarian notation:
    - the type of a variable may change several times during the lifetime of a project; but now changing the type involves adapting every occurrence of that variable.
    - a less-than-gifted programmer (the sole reason for Hungarian notation) may choose the wrong prefixes, confusing everyone who has to read his code.
    - ...

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The name of a variable should indicate its purpose, not its type. If the type must be known, it can usually be inferred from the purpose, which should already be described in the name.

    For instance, a variable "FirstName" probably contains a person's first name, and it thus probably a string. If it isn't, the confusion should be addressed not by calling out the weirdo type, but by changing the variable's name to something more appropriate.

    Not all affixes to variable names are bad. If these affixes help explain the purpose of the variable as opposed to its type, they can help reduce keyboard typing and effort when naming variables as long as they are applied consistently.

    Prefixing members with "m" makes it clear that they are members, which goes to "explaining purpose." Prefixing counters with "n" explains that they are maintaining a count of something which is described by the rest of the name.

    Arrays might be named "FirstNames" with the final "s" acting as a suffix meaning "array" but sometimes this conflicts with normal English usage, so you might also prefix with "a".

    Pointers might be prefixed with "p".

    I don't consider "pointer" or "array" to be types, obviously. After all, you can't declare something with type "pointer," only type "pointer to <type>", and the same for an array.

    On the other hand, encoding that a variable is a "long integer" in its name doesn't help you understand its purpose, and forces you to rename the variable if you later decide that it should have been "unsigned long integer."

    There are gray areas of course. If something is a 16-bit quantity, and could realistically be expected to overflow because of its limited range, you might describe this in the name so that on every usage of that variable the programmer is considering the consequences.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CamelCase VS Hungarian notation, which is better?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2007, 09:31 PM
  2. Hungarian Notation
    By FOOTOO in forum C Programming
    Replies: 6
    Last Post: 05-20-2005, 08:35 PM
  3. Hungarian Notation POLL
    By maxhavoc in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-21-2004, 10:52 AM
  4. hungarian notation
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2003, 01:19 PM
  5. Hungarian Notation
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 10-13-2001, 11:17 AM