Hungarian notation bad

Hem / Teknik & Digitalt / Hungarian notation bad

Limited length of names didn't help either, so all those "ls_" had some significant advantages.

But times have changed. At first glance, identifiers using Hungarian Notation appear to be gibberish until the pattern is deduced. What's weird - prefixes are more required from the freshman than proper naming in general.

Because of the reasons above, one person in a program might use wa_ (making it unclear if the variable is local or global), while another uses ls_ or gs_.

  • qualifier: the remainder of the name of the identifier describes what the variable is used for. You’ll still be able to read and write it.

    However, giving up HN won’t noticably improve your code unless it’s part of a broader set of changes, including:

    • Meaningful naming (without HN)
    • Small routines (methods)
    • Single responsibility

    This short list is just the beginning, but following these three principles alone (along with giving up on prefixes) will significantly improve the quality and maintainability of your code.

    If not HN, then which style should I follow?

    What is gt_partner in this case? If the frist and last letters of a word are correct and the middle letters are slightly jumbled, we can still read it as if it were correct, sometimes without even notciing the errors 😉.

    Common Type Prefixes

    | b | by y | c | C | d | dw | f | g_ | h | I | l | m_ | n | p | s str | sz psz | u | v | w | x | X |

    PrefixMeaningExampleNotes
    pPointerIn most cases, p is combined with another prefix; the prefix of the type of object being pointed to.

    I know I’m looking for a table of customers, so I type "cus" and hit ctrl+Space. If it is not possible to abbreviate and the type name is not too long, you can just use the type name as a prefix.

    The Type Prefix will always be entirely lowercase and should reflect the name of the type by abbreviating it distinctively.

    In Systems Hungarian, the only thing that the prefix told you was the actual data type of the variable.

  • The misunderstanding and subsequent rebellion against Hungarian notation
  • Making Wrong Code Look Wrong:

    • Use naming conventions that make errors visually obvious
    • Collocate relevant information in code for easier comprehension
  • Critique of Exceptions:

    • Exceptions can break code collocation
    • May make it harder to visually identify potential issues
  • Key Takeaways

    1. Coding conventions should help programmers visually identify potential errors.
    2. Collocating relevant information in code improves readability and maintainability.
    3. Apps Hungarian notation can be valuable for indicating semantic meaning.
    4. Systems Hungarian notation is less useful and led to a backlash against Hungarian notation in general.
    5. Simple tools that account for human fallibility may be preferable to complex ones with hidden side effects.

    The article emphasizes the importance of making code visually intuitive and easy to debug, particularly for mission-critical applications[1][2].

    Origins

    The notation was invented by Charles Simonyi, a Hungarian-born programmer who worked at Xerox PARC and later became Chief Architect at Microsoft.

    Sure, if you’re used to hieroglyphics, you can read them quickly, but it’s still much slower than reading a regular novel.

    As Uncle Bob once said, adding prefixes is like adding unnecessary code to the code itself. Now, I "search" for variables by their name, not their type.

    Example:


    More explanation about Hungarian notation

    .

    Your programs should clearly outline the purpose of the variable along with its declaration . Is it a field symbol, or a structure? There is a set of standard qualifiers for variables used in commonly performed programming tasks.

    QualifierExplanation
    A temporary from which the value will be restored
    A value that follows one behind a current value in an iteration (eg.

    The new names were:

    • gt_vbak = orders
    • gt_vbak2 = quotations
    • lt_vbak1 = orders_wo_quotation (in one routine, all orders not originating from quotations were extracted and processed)
    • lt_vbak2 = rejected orders -yes sir!

      Since its inception in 1972, Hungarian Notation has been adopted by Xerox, Apple, 3Com, and of course Microsoft.

      For example:

      is a pointer to a string object containing a name.

    s
    str
    StringThis convention is generally used for first-class string classes.
    sz
    psz
    zero-terminated / null-terminated string
    hHandle
    cCharacter (char)Sometimes c is used to denote a counter object.
    by
    y
    Byte or Unsigned Char
    nInteger (int)
    fFloat
    dDouble
    bBooleanAn integer can store a boolean value as long as you remember not to assign it a value other than 0 or 1
    uUnsigned...
    wWord or Unsigned Integer 
    lLongSometimes l is appended to p to denote that the pointer is a long.

    Harm #1: It Encourages Laziness and poor Naming

    In engineering, every solution involves trade-offs—it's about weighing the pros and cons.

    hungarian notation bad

    wa_, or ls_? The same goes for types - I never trust the letter and I check the type by myself (F2 in Eclipse). The g_ would denote that a particular global is not a constant.

    vVoid (no type)In most cases, v will be included with p because it is a common trick to typecast pointers to void pointers.

    Top of table

    Type Prefixes for non-common Types

    In many cases, you will have identifiers of non-standard types.