PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

C++ / MFC Naming Conventions

Hi. It looks like we have a lot of new developers starting up, so I thought I would very quickly outline some of the standard naming conventions. I've seen source where people did not understand these conventions, and it made the source terribly confusing. I also have never seen any guide that consolidated all these naming conventions, so I thought I would whip one up.
Most of this is related to MFC, but you should still use these conventions for general C++. The main idea of these naming conventions is to determine information about a variable or type without having to consult the header file it was defined in.

Classes
Class names should start with a capital C, ie:
CMyClass, CAWindow, CCustomButton
Note that the header and source file names do not start with C:
MyClass.cpp, MyClass.h, AWindow.h, etc.
Also note that when you create a new class with ClassWizard, if the class name starts with C it will strip that C off of the filenames. So if you do not use the above convention, and try to create a new class such as "Car", ClassWizard will use the filenames "ar.h" and "ar.cpp" by default.

Variables
Usually variable names, particularly members of a Class, are preceded by lower case letters to indicate what type the variable has. This is called Hungarian Notation. For example:
Code:

int    nCount;
LPTSTR pszName
TCHAR  szAddress[256];
int    *pnTotal;
HANDLE hWnd;
HDC    hDC;


You can see that p indicates a pointer, n = integer, sz = string (the z means it is zero terminated), h = Windows Handle, etc.
Hungarian Notation is a little less useful when you have powerful development GUIs, like eVC++ and VC++, where you can just hover your mouse over a variable name to see how it is defined. You can imagine the old days when all code was written in a simple text editor. Hungarian Notation saved a great deal of time in those days (and still can).

Class Variable Members
One of the most important conventions is the naming of a Class' member variables. These should be preceeded by m_ ie:
Code:

int    m_nCounter;
LPTSTR m_pszFirstName;

This is important for a few reasons. Primarily it gives you greater freedom in your member functions so you don't have to worry that you create a variable that is already a class member. Imagine if you have a class that is derived from several others, and one of the base classes has a member variable "i". If you used a local i in some functions, then it wouldn't always be clear which i was being referred to. Conversely, never prefix a local variable with m_!  I downloaded some source once where the author was obviously unclear what the m_ represented, and created some local variables with that convention. It resulted in a confusing mess.

Well, those are the biggies. I try to use the Hungarian Notation when I can, but I still slip from time to time. Certainly the most important is to precede class member variables with "m_"!

Dan East
Moose or Chuck · 14 Jul 2001, 11:06 UTC
Wow, just saved me from writing that up on one of my tutorials. Dan, can I use this short little essay of yours on my PocketCoder website?
Credit given where due of course.
Yep.

Dan East
People would even want to read the book "Code Complete" written by Steve McConnell. It is the bible of writing lovable code :)
Moose or Chuck · 15 Jul 2001, 09:28 UTC
Image
Here is the link if anyone wants to buy it, or just look at it's details. It seems like quite a nice book. It's too bad I have so much time on my hands, which everyone tries to make me use their way so I don't have any time to do what I want anymore.
I have no idea why this thread insists on being "New!"
Moose or Chuck · 16 Jul 2001, 01:32 UTC
well its not. Boy i wish i could post html here. but that will have to wait...
Moose or Chuck · 16 Jul 2001, 14:04 UTC
Probably because i modified that last post there. I should really go in there and fix that bug, since Chris and Matt are too ignorant in the ways of Perl. But that involves work, and investigation, and reading... None of which suit me well.
Not gonna make friends today.

Dan.  Nice list, but I have a problem with
LPTSTR pszName;
TCHAR  szAddress[256];

BOTH are pointers.  I typically use sz in either case, since it is more indicative of use than datatype.

n for int....what happened the old stand-by of i?

And p for pointer is ok, but I am a bad boy from the old school who still uses lp.

m_  EVERYONE should do this...glad you said it too.

MooseMaster...the book is great.  I have a copy that I think I wore the ink off of.  ALSO GREAT BOOKS.

Anything by Knuth.
Writing Solid Code.
Code. (I forget who it is by, but it has everything about bits and bytes and switches you can imagine.  He starts out with creating a basic switch from an iron rod and some coil...heheh)
Technically both are pointers, but you do not assign a value to an array as you do a char pointer:
Code:

LPTSTR pszTest;
LPTSTR pszTest2;
TCHAR  szBuf[256];

//Okay:
pszTest2=szBuf;
pszTest=pszTest2;

//Not cool:
szBuf=pszTest2;


The naming convention I suggested allows you to differentiate by the variable name which is an array and which is actually a pointer. That of course is the purpose of Hungarian Notation - determining the type of variable by its name.

Dan East
I hate being wrong.  I went digging around in our standards list at work and it tends to agree with you.  Damn. :(
Some coding starndards we use for variable naming goes a little further. For local variables, we put 'L' at the end. For arguments to a function, we put 'A' at the end. This helps tremendously. Also, for global functions, we add a 3 letter abreviation of the part of the program it is in. For instance, in the model library part, the global functions start with MLF_. Which stands for Model Library Function.
Wow, I'm surprised people are reading my posts that are 1.5 years old. :) I've updated the tags for the current php BBS software.

Dan East
Hungarian notation has both advantages and disadvantages.

1) It has the general advantages of having a naming convention.
2) It serves to document types in a weakly typed language. This doesn't apply as much for C++ as it does for C.

But Hungarian has serious drawbacks:

1) Hungarian ignore the use of abstract data types as base types. Because of this, it forces programmers to worry about manual type checking instead of letting the compiler check the types more rapidly and accurately.
2) It combines data "meaning" with data "representation". This is so bad, I don't even know where to start.
3) It encourages lazy, uninformative variable names. (ie: "hWnd" vs "MainMenuWindow").

These ideas are taken directly from "Code Complete" mentioned up there, and I totally agree with the author's point of view. Hungarian simply sux. It isn't all that bad when programming in C, but it is a real problem when dealing with C++. It encourage slopiness in the architecture. If you want a good example of a crappy architecture, look no further then MFC. It's an old library that was written before C++ had a standard. I am not saying MFC wasn't good when it came out, it is simply a product of it's time.

Now prefixing all class names with "C" or even "T" or whatever is not that bad, although I prefer to start my type definitions with an uppercase letter. This way, the prefixing is not needed.

To anyone who will argue that having the type described as a prefix "pwstr" is useful because you don't have to go check the type of a variable when you use it: that's exactly why prefixes shouln't be used. To me, "playerName" is infinitely more clear then "pszname", and "pszPlayerName" is not adding anything. It even become a problem when I change my variable to a "std::string".
Sorry, I didn't look at the date. I'm new to the board and was just reading through the posts.
Hey, that's no problem. It's a Good Thing that people are actually reading the old posts.

Dan East