C/++ Function Naming Conventions

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

C/++ Function Naming Conventions

Post 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?
User avatar
BugInTheSYS
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 88
Joined: Mon Feb 07, 2011 4:16 pm
Current Project: Vintage Roads
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, Delphi
Location: Pinneberg, Germany
Contact:

Re: C/++ Function Naming Conventions

Post 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
Someday, everything will go to /dev/null. - Bug's prophecy 13:37
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: C/++ Function Naming Conventions

Post 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.
pubby8
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 58
Joined: Mon Feb 07, 2011 12:22 am

Re: C/++ Function Naming Conventions

Post by pubby8 »

Just keep it consistant.

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

Most c++/c uses lowercase and "_"
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: C/++ Function Naming Conventions

Post 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:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: C/++ Function Naming Conventions

Post by eatcomics »

same for me Gino. Although I've not made a library.
Image
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: C/++ Function Naming Conventions

Post 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().
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: C/++ Function Naming Conventions

Post 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;
};
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: C/++ Function Naming Conventions

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: C/++ Function Naming Conventions

Post 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..?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: C/++ Function Naming Conventions

Post 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.
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: C/++ Function Naming Conventions

Post 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.
Image
Post Reply