Sign in to follow this  
Jonus

C++, naming member functions

Recommended Posts

Some question about naming:
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 this post


Link to post
Share on other sites
Quote:
Original post by Jonus
Problem 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 this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by Jonus
Problem 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 this post


Link to post
Share on other sites
Quote:
Original post by Jonus
Quote:
Original post by Fruny
Quote:
Original post by Jonus
Problem 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 this post


Link to post
Share on other sites
Quote:
Original post by Jonus
What do you prefer?


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

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by CJWR
i 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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by fireside
Stay 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 this post


Link to post
Share on other sites
Quote:
Original post by cilcoder
Method 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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this