# C++, naming member functions

## Recommended Posts

Jonus    170
class A
{
public:
void Name(){}
}

class B
{
public:
void name(){}
}


What do you think is better Name or name? Problem is afaik in the book Designpatterns they use UpperCase notion, but in the stl they use lowercase. What should i do? Is Stl naming the final truth?

##### Share on other sites
Fruny    1658
Quote:
 Original post by JonusProblem is afaik Designpatterns use UpperCase notion and stl uses lowercase. What should i do?

Pick one or the other for yourself and use it consistently. Anything beyond that is a question of personal preference.

##### Share on other sites
Jonus    170
Quote:
Original post by Fruny
Quote:
 Original post by JonusProblem is afaik Designpatterns use UpperCase notion and stl uses lowercase. What should i do?

Pick one or the other for yourself and use it consistently. Anything beyond that is a question of personal preference.
What do you prefer?

##### Share on other sites
Agony    3452
Quote:
Original post by Jonus
Quote:
Original post by Fruny
Quote:
 Original post by JonusProblem is afaik Designpatterns use UpperCase notion and stl uses lowercase. What should i do?

Pick one or the other for yourself and use it consistently. Anything beyond that is a question of personal preference.
What do you prefer?
That you "pick one or the other for yourself and use it consistently." [wink] I personally prefer uppercase, if my opinion amounts to anything.

##### Share on other sites
Fruny    1658
Quote:
 Original post by JonusWhat do you prefer?

I generally use lower_case_with_underscores for members and reserve UpperCaseWords for class names.

##### Share on other sites
LostSource    199
I prefer to put a capital C in front of all my class names, and for function names I will have the first word in the function name be lower case and the rest of the words upper case. And for varibles I use an underscore between words and keep them all lower case.

class CEntity{    public:       CEntity();       void thisIsAnExampleFunction();    private:       int example_varible;};

##### Share on other sites
Im with the camelCaseCrowd just for the record. But I tend to use UpperCase for free functions for stuff akin to generic/functional programming I go with the stl convention of using lowercase.

Maybe Im weird?

##### Share on other sites
CJWR    180
i use all upper case for class types, and all lower case for everything else:

class TEST{
void so_fuction;
};

TEST test;

##### Share on other sites
Jonus    170
Quote:
 Original post by CJWRi use all upper case for class types, and all lower case for everything else:class TEST{ void so_fuction;};TEST test;
Most ppl use all upper case for #defines.

##### Share on other sites
demonkoryu    980
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.

##### Share on other sites
Jonus    170
Quote:
 Original post by KonfusiusI useUpperCamelCase 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?

##### Share on other sites
demonkoryu    980
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 programmingThe 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.

##### Share on other sites
MaulingMonkey    1730
(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.

##### Share on other sites
Telastyn    3777
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.

##### Share on other sites
Zahlman    1682
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. :)

##### Share on other sites
helix    301
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;

##### Share on other sites
Guest Anonymous Poster
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!

##### Share on other sites
helix    301
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.

##### Share on other sites
cilcoder    122
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

##### Share on other sites
fireside    122
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.

##### Share on other sites
Telastyn    3777
Quote:
 Original post by firesideStay away from underscores. Sure, they look nice, but you have to reach way up there with your little pinky all the time.

I don't know about proper typists, but shift alone is the pinky hitter for me. That's why I dislike the CamelCase. Underscore is 'only' ring+middle of right hand.

##### Share on other sites
demonkoryu    980
Quote:
 Original post by cilcoderMethod Parameters: Prefixed with lowercase v. Example: DoSomethingCool(int vMyParameter)

In the case of parameters, I would prefix them with 'a' rather than 'v'.
Example: Print( string aString )

##### Share on other sites
cilcoder    122
I'm not sure how are started the 'v' thing but I prefer it rather than other letters now that I think about it. I think it looks "good" not sure how or why.. I kinda like v because it makes it where I won't try to read it as a word. If I named a variable aBoy that would make me read it as "A Boy" instead of Boy which is a parameter. or as a better example if I was passing something like a parameter named, Objects. I prefer vObjects instead of aObjects(because A Objects would annoy me :-D). Just my personal preference though and it typically changes over the years, the more I read other peoples code, etc.