Search code examples
conventionshungarian-notationself-documenting-code

Do people use the Hungarian Naming Conventions in the real world?


Is it worth learning the convention or is it a bane to readability and maintainability?


Solution

  • Considering that most people that use Hungarian Notation is following the misunderstood version of it, I'd say it's pretty pointless.

    If you want to use the original definition of it, it might make more sense, but other than that it is mostly syntactic sugar.

    If you read the Wikipedia article on the subject, you'll find two conflicting notations, Systems Hungarian Notation and Apps Hungarian Notation.

    The original, good, definition is the Apps Hungarian Notation, but most people use the Systems Hungarian Notation.

    As an example of the two, consider prefixing variables with l for length, a for area and v for volume.

    With such notation, the following expression makes sense:

    int vBox = aBottom * lVerticalSide;
    

    but this doesn't:

    int aBottom = lSide1;
    

    If you're mixing the prefixes, they're to be considered part of the equation, and volume = area * length is fine for a box, but copying a length value into an area variable should raise some red flags.

    Unfortunately, the other notation is less useful, where people prefix the variable names with the type of the value, like this:

    int iLength;
    int iVolume;
    int iArea;
    

    some people use n for number, or i for integer, f for float, s for string etc.

    The original prefix was meant to be used to spot problems in equations, but has somehow devolved into making the code slightly easier to read since you don't have to go look for the variable declaration. With todays smart editors where you can simply hover over any variable to find the full type, and not just an abbreviation for it, this type of hungarian notation has lost a lot of its meaning.

    But, you should make up your own mind. All I can say is that I don't use either.


    Edit Just to add a short notice, while I don't use Hungarian Notation, I do use a prefix, and it's the underscore. I prefix all private fields of classes with a _ and otherwise spell their names as I would a property, titlecase with the first letter uppercase.