I am working on an application which uses Win32/MFC framework and C++ programming language using Visual Studio 2010.
My question is which naming convention needs to use for variables, function name, class name and etc. I heard Microsoft suggest to use “Hungarian notation” naming convention.
Can you please let me know which standard to use?
You might have a truly good reason to want your code to look familiar to other programmers who have learned MFC. If so, then you could emulate the conventions of the MFC Samples and Tutorials, and do what they do...
But MFC is a legacy library which originated very early, and times have changed. Microsoft's style guidelines now specifically say NOT to use Hungarian notation:
http://msdn.microsoft.com/en-us/library/ms229042.aspx
That aside, rather than blindly following any particular document, it is probably better to evaluate each individual convention. For instance, when MFC samples make classes and have member variables, they start the name with m_
. StackOverflow has questions about this particular idea and you can read about alternatives:
Why use prefixes on member variables in C++ classes
I'm more aligned with Qt's API Style Guidelines, and most of their conventions work for me:
http://doc.qt.nokia.com/qq/qq13-apis.html
Speaking of which, Qt is a nice C++ offering that has continued to evolve and mature. Blows MFC out of the water (completely) AND is cross-platform...
http://qt.nokia.com/products/developer-tools/
If you want to be locked in the Microsoft universe go for .NET and C#. That would be a step up, and you wouldn't be stuck with the MFC abandonware.
Oh boy. That's almost like asking people what religion you should follow. At least you didn't ask what text editor you should use...
My 2c: do what works for you. Also consider whether you are just coding for yourself, or whether you expect other people to read or work on the code you write, and whether you are starting a project from scratch, or building on existing code.
Naming convention really doesn't matter much, so long as:
You pick names that are meaningful and describe the purpose of the variable/function/other. (Key issue here is purpose; let the compiler deal with the type.)
Be consistent in how you apply any other aspects of the convention (indentation, casing, prefixes, use of underscores, etc).
Generally speaking, if code is well-written, the details of the convention don't matter much.
As for Hungarian and prefixes: Win32 and C++ still uses them somewhat, while .Net and C# don't.
I'd can highly recommend reading this long but very insightful article by Joel Spolsky that goes to great length to outline how and why a prefix convention can be actually very useful, if done correctly, and also where it's just a pointless chore.
These days, I don't bother with most Hungarian prefixes, except for a few (note that these are just my personal preferences, they're ones that I've found to be useful over the years):
- p for pointers, because in C++, unlike C#, it's useful to know when you're dealing with a reference vs object, and how many levels of indirection I'm dealing with.
- m_ for member variables (Sometimes _ in C# depending on existing code; see HostileFork's note below re _ as a prefix in C++.)
- cch for character counts, cb for byte counts. Its really important in Win32 to not get these mixed up; pass a character count to memcpy or a byte count to GetWindowText, and you'll have trouble. This is the sort of use of prefixes that can help keep you code clear and obviously correct (or incorrect, as the case may be - "ah, of course, I'm passing a cch to memcpy, that's the problem!").
For me, the first two of these help make the code more readable; the third example here helps both with readability, but can also be a useful technique for ensuring correctness - a bit like a mnemonic, if you will.