C++, naming member functions

Started by
21 comments, last by cilcoder 18 years, 9 months ago
Quote:Original post by Konfusius
I use

UpperCamelCase for classes,
lower_case for structs, member and local variables and generic code,
and lowerCamelCase for private/protected functions,
and ALL_UPPER_CASE for non-local consts and #defines.
Konfusius do you know the tao of programming?
Advertisement
Why yes, I've read that. Are you asking because I nicknamed myself Konfusius?
If so, I surely don't want to imply that I am a guru of some kind, my intention was to express that I am konstantly learning new things [grin] and that I am konfused sometimes. [looksaround]
Quote:from the TAO of programming
The wise programmer is told about Tao and follows it. The average programmer is told about Tao and searches for it. The foolish programmer is told about Tao and laughs at it.

(For C++) I use:

UPPER_CASE_WITH_UNDERSCORES for macros and other evil #define magic.
lower_case_with_underscores for everything else, based on existing practice as set by the SC++L.
Quote:Original post by MaulingMonkey
(For C++) I use:

UPPER_CASE_WITH_UNDERSCORES for macros and other evil #define magic.
lower_case_with_underscores for everything else, based on existing practice as set by the SC++L.


Ditto, although I push global enum labels into UPPER_UNDERSCORED.
Almost anything you've seen will be ok, as long as you're consistent about it. Although personally, I would avoid anything that involves adding extra letters or underscores to indicate "what something is". If you can't easily find the definition, and the name doesn't clearly indicate what you're dealing with, then your code is too complicated for what it does, and the name could be better. :)
This is my preferred style in a nutshell.

static const int   kConstant1 = 0;class CClassName{public:    CClassName( void );    ~CClassName( void );    void    FunctionName( int nParam1, float fParam2 );protected:    int     m_nMemberVar1;    float   m_fMemberVar2;    int*    m_pMemberVar3;};static int s_nStaticVar1;int        g_nGlobalVar1;
I capitalize global stuff and use all lower with underscores for member stuff. All caps for constants and the like. So a non member function starts with a capital letter, a struct starts with a capital letter.

This system allows me to reuse words. You don't always want to come up with a long variable name. Let's say that I have a type Cat and I want to make a temp Cat variable. I could call it temp, but that is horrible. Instead I call it cat. I can use case to tell apart variable from type.

A lot of people waste the ability to do this by using capital for types and lowercase for functions or something like that. However in general if you have trouble telling apart types (nouns) from functions (verbs) you need to learn how to name stuff better.

Using C to mean class is just poor design. I strongly approve of using type information in variable names. Let's say that you have a game where each player has a different color. You store the colors as unsigned ints and in this case you have an array of them. Some people might try to pass off some silly name such as auiPlayer as a good name, meaning array of unsigned ints. However while the computer is dealing with an array of unsigned ints, you are dealing with a collection of colors. Thus PlayerColors is a superior name. You have the type, which is Color, and you have it as plural to indicate that you are dealing with something plural.

Of course while coding conventions are great, what happens when you want to name your variable ID, meaning identification? Do you go with player_ID or player_id? I would go with the first even if your convention suggests the second. Remember to use your common sense!
One more thing I should mention. All member variables MUST be set apart in some way from other local (etc) variables (I prefer m_... but anything consistant is fine). Not only is this less error prone, other people who need to maintain your code will be able to easily see what's going on. When I have to look in someone's old project to add something to it, and their members are all named identically to their local variables, I want to strangle them! Their code is so difficult to read and make sense of.
Methods: Uppercase. example: DoSomethingCool()
Class Instance Fields/Variables: has lower m prefix. Example: mMyVariable
Class Fields/Variables: Prefixed with an s. Example: sMyClassVariable
Properties(not applicatable in C++): UpperCase. Example: MyNumber
Class Properties(not applicable in C++): Still haven't decided yet, considering an Uppercase S or just treat them the same as properties... Don't use them enough to make a decision.
Method Parameters: Prefixed with lowercase v. Example: DoSomethingCool(int vMyParameter)
Local Variables: lower case, each new word uppercase. Example: myLocalVariable.
Global Variables: EVIL! :-P
My 2 cents. Stay away from underscores. Sure, they look nice, but you have to reach way up there with your little pinky all the time. If you only use first capital letters for class names, you won't have to type the stupid C all the time. Use camel hump to seperate words. Use plurals for vectors or containers. Most importantly, use names that tell something useful about the property or function, like what it is or what it does. Splurge on this, type a few extra words. There's nothing more important for readable code.

This topic is closed to new replies.

Advertisement