Page 1 of 1

C/++ Function Naming Conventions

Posted: Mon Feb 07, 2011 4:46 pm
by ajtgarber
I remember reading somewhere that the convention for naming functions is to leave the first word uncapitalized, then the following words would be capitalized like:

Code: Select all

void func();
void myFunc();
but I've seen in various places functions like this:

Code: Select all

void func();
void my_func();
//OR
void Func();
void MyFunc();
What is the convention for naming functions?

Re: C/++ Function Naming Conventions

Posted: Mon Feb 07, 2011 4:55 pm
by BugInTheSYS
So, let's have a look at this (in this case google was my friend, and it would also have been yours ;) )
http://geosoft.no/development/cppstyle.html
It says:
6 - Names representing methods or functions must be verbs and written in mixed case starting with lower case.

Code: Select all

getName(); 
computeTotalWidth();
[...]
9. Abbreviations and acronyms must not be uppercase when used as name.

Code: Select all

exportHtmlSource(); // NOT: exportHTMLSource();
openDvdPlayer();    // NOT: openDVDPlayer();
For further detail, look at the page, it contains about 100 conventions whose breakdown would've taken pretty long :D
Hope I could help!
Bye

Re: C/++ Function Naming Conventions

Posted: Mon Feb 07, 2011 4:57 pm
by XianForce
ajtgarber wrote:I remember reading somewhere that the convention for naming functions is to leave the first word uncapitalized, then the following words would be capitalized like:

Code: Select all

void func();
void myFunc();
but I've seen in various places functions like this:

Code: Select all

void func();
void my_func();
//OR
void Func();
void MyFunc();
What is the convention for naming functions?
From what I understand it's a personal choice. The first method is called lower camel case and the second upper camel case. Although it is a thing of preference, generally, I see lower camel case used for global functions, and upper camel case used for methods of a class.

Re: C/++ Function Naming Conventions

Posted: Mon Feb 07, 2011 5:37 pm
by pubby8
Just keep it consistant.

CamelCase is usually for java, but I prefer it in c++.

Most c++/c uses lowercase and "_"

Re: C/++ Function Naming Conventions

Posted: Mon Feb 07, 2011 7:50 pm
by Ginto8
My personal naming conventions depend on whether it's in a library or just a normal program:
Functions:
  • Library: Normal:
Classes:
  • UpperCamelCase:

    Code: Select all

    class MyClass {};
Methods:
  • Library:
    • UpperCamelCase:

      Code: Select all

      class MyClass { void MyMethod(); };
    Normal:
    • lowerCamelCase

      Code: Select all

      class MyClass { void myMethod(); };
Variables:

Re: C/++ Function Naming Conventions

Posted: Mon Feb 07, 2011 9:16 pm
by eatcomics
same for me Gino. Although I've not made a library.

Re: C/++ Function Naming Conventions

Posted: Mon Feb 07, 2011 10:12 pm
by avansc
Just as an addition, I like adding some form of hungarian notation

cClassName or sStructName

pPointerName // one I learned from qp, g_pGlobalPointerName;

for function names its usually just camel case, but sometimes I deviate.

getX() or get_x().

Re: C/++ Function Naming Conventions

Posted: Tue Feb 08, 2011 6:57 am
by adikid89
I hate Hungarian notation.. it's useless nowadays...
I like to use upper camel case, and m_ for member data and s_ for static member data... The reason is because when you type m_ or s_ you get the member data with intellisense(or similar systems) so it'll make life a tad easier. For small get/set functions is use low case though..

Code: Select all

class Class 
{
   public:
       Class(): m_data(0) {}
       inline int size() const { return m_size; }
       inline void SetSize(int size) { m_size = size; }

   private:
       int m_size;
};

Re: C/++ Function Naming Conventions

Posted: Tue Feb 08, 2011 7:20 am
by N64vSNES
Depends on what I'm doing really.

Functions/Classes/Namespace etc

Code: Select all

void MyThing();

Code: Select all

int MyThing();
But for variables I like to use hungarian notation

Code: Select all

int iNumber = 0;

Code: Select all

std::string sString;
Although this probably isn't the professional way of naming.

Re: C/++ Function Naming Conventions

Posted: Tue Feb 08, 2011 9:36 am
by avansc
adikid89 wrote:I hate Hungarian notation.. it's useless nowadays...
I like to use upper camel case, and m_ for member data and s_ for static member data... The reason is because when you type m_ or s_ you get the member data with intellisense(or similar systems) so it'll make life a tad easier. For small get/set functions is use low case though..

Code: Select all

class Class 
{
   public:
       Class(): m_data(0) {}
       inline int size() const { return m_size; }
       inline void SetSize(int size) { m_size = size; }

   private:
       int m_size;
};
Well if you wanna talk about useless, you might wanna look at how inline works.. those functions (size() and SetSize(int)) are implicitly inline already so saying they are inline is just redundant.. Its like doing "register int a" instead of "int a", the compiler will put it in the register regardless of what you ask it to do, "nowadays..."(this is not true for old compilers)

secondly, even if they were not, the compiler is smart enough "nowadays..." to know when to make a function inline, and that putting the keyword inline does not mean it will be inline(its a request, not a mandate). Also, if you inline a lot, your code gets big, and you can run into thrashing issues. If you force your inlines, you WILL run into thrashing issues.

And lastly, dont you think that your naming convention is a form of hungarian notation, you place those preceding characters to tell you something about the object to make it self evident what they type or scope is..?

Re: C/++ Function Naming Conventions

Posted: Tue Feb 08, 2011 11:03 am
by adikid89
avansc wrote: Well if you wanna talk about useless, you might wanna look at how inline works.. those functions (size() and SetSize(int)) are implicitly inline already so saying they are inline is just redundant.. Its like doing "register int a" instead of "int a", the compiler will put it in the register regardless of what you ask it to do, "nowadays..."(this is not true for old compilers)

secondly, even if they were not, the compiler is smart enough "nowadays..." to know when to make a function inline, and that putting the keyword inline does not mean it will be inline(its a request, not a mandate). Also, if you inline a lot, your code gets big, and you can run into thrashing issues. If you force your inlines, you WILL run into thrashing issues.

And lastly, dont you think that your naming convention is a form of hungarian notation, you place those preceding characters to tell you something about the object to make it self evident what they type or scope is..?
Yes.. actually I did know that... but still I like to explicitly inline them.. looks cleaner(that is.. i like it better that way). And yes, I know what the hungarian notation is used for, but I don't want to right a i or something before every variable, when I can easily check that with by running the cursor over a variable/function/class/whatever. I used m_ and s_ for intellisense...

Hungarian notation makes stuff harder to read, especially that CClass thingy.. though indeed it you can easily tell what type a variable is. Overall I hate it.

Re: C/++ Function Naming Conventions

Posted: Tue Feb 08, 2011 9:17 pm
by eatcomics
Not that I hate hungarian or don't see the uses, I just personally don't use it, as I agree with inline that I can easily find out the type. I also find that I find myself mistyping a letter and being like "Why the hell isn't that a function?" But that's mostly because I suck at coding in general.