Do I have to follow the Hungarian Notation in Windows programming?

燕鹏 李 301 Reputation points
2022-04-28T14:31:44.1+00:00

Honestly, I am tangled about this issue. I used to add m_ to the beginning of the member variable, and g_ to the beginning of the global variable, at most, add C to the beginning of the class name. But whether it is the Windows programs (C/C ++) or Windows programming books, almost all variables are using Hungarian naming methods. Is it really easier for cxChar to make people understand than charWidth? Even if hInstance, I want to use instanceHandle instead.

In Hungarian notation, we encode the type information into the variable name, then decode this information when reading the codes, why not use more descriptive words to name a variable directly?

Do you also follow the Hungarian Notation strictly when writing the Windows programs?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,676 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,780 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 55,301 Reputation points
    2022-04-28T15:14:09.857+00:00

    Hungarian notation is a legacy tool that was used back in the days of simple text editors and block declaration languages. Back then if you saw an identifier such as m_iHeight you could tell it was a field of a class and an integral. It was a productivity tool. However we have long since moved to smart editors where you can generally hover over the identifier and see the same information. This makes Hungarian notation mostly useless.

    It was never pretty before and in cases where compiler memory space was small this really hurt naming conventions (hence the x identifiers). Again, not an issue these days so use solid noun-based identifier names without any notation, with a couple of exceptions.

    • In pretty much every language that supports interfaces it is common to precede the interface with "I". So IEnumerable or IDisposable are interfaces.
    • Most people prefix _ on field names. This is just to avoid name collisions for common field names that might overlap parameter names so we can reduce the need for using this on names. private int height; //Not preferred
      private int _height; //Preferred
      void Foo ( int height )
      {
          this.height = height;  // This required so compiler knows we are using field
          _height = height;       // Cleaner
      }
      

    Ultimately I'm not aware of any modern language that has any naming rules for identifiers in terms of notation. So you should follow whatever style is already in use for the code you're maintaining. If this is new code then skip the Hungarian notation in general.


2 additional answers

Sort by: Most helpful
  1. RLWA32 45,866 Reputation points
    2022-04-28T14:40:20.027+00:00

    Hungarian notation is NOT a requirement. It was probably more useful many years ago before our tools became capable of informing us about variable types simply by hovering the mouse over them. :) Still, sometimes conventions are useful and I tend to stick with m_ for member variables and g_ for globals. In the absence of things like corporate standards requirements its about personal preference.


  2. Castorix31 86,071 Reputation points
    2022-04-28T14:47:39.72+00:00

    Most of MS SDK C/C++ samples use Hungarian notation for more than 30 years, so when you use some parts of their code or copy some utility functions, it is more readable in your code if you use it too
    And with just the 1/2 first characters of a variable, you see immediately its type


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.