Function names in C ++: capital letters or not?

What is the convention for naming functions in C ++?

I came from a Java environment, so I usually call something like:

myFunction(...) { } 

I saw mixed code in C ++,

 myFunction(....) MyFunction(....) Myfunction(....) 

What is the right way?

Also, is it the same for a class method as well as for a method other than a class?

+58
c ++ function coding-style naming-conventions
Nov 21 '09 at 18:24
source share
12 answers

There is no “right way”. They are all syntactically correct, although there are some conventions. You can follow the Google style guide , although there are others.

From the mentioned guide:

Regular functions are mixed case; access methods and mutators correspond to the variable name: MyExcitingFunction (), MyExcitingMethod (), my_exciting_member_variable (), set_my_exciting_member_variable ().

+39
Nov 21 '09 at 18:27
source share

Starting with C ++ 11, you might want to use snake_case or camelCase for function names.

This is because in order for a class to work as a range expression in a for loop based on a range, for this class it is necessary to define functions named begin and end (case sensitive).

Therefore, using, for example, PascalCase for function names means that you need to break the consistency of names in your project if you ever need to get the class to work with a range based.

+34
Aug 08 '15 at 13:48
source share

Most of the codes I've seen are camelCase functions (lowercase initial letter) and ProperCase/PascalCase class names and (most usually) snake_case variables.

But honestly, this is all just a guide. The most important thing is to be consistent in your code base. Choose what seems natural / works for you, and stick to it. If you are joining a project under development, follow their standards.

+24
Nov 21 '09 at 18:28
source share

The most common of the production code (in that order):

 myFunctionName // lower camel case MyFunctionName // upper camel case my_function_name // K & R ? 

I find that the naming convention that a programmer uses in C ++ code usually has something to do with their programming language.

eg. ex-java programmers tend to use a lower camel case for functions

+15
Nov 21 '09 at 18:40
source share

If you look at the standard libraries, the template is usually my_function , but each person seems to have their own way: - /

+11
Nov 21 '09 at 18:28
source share

Personally, I prefer thisStyle - thisStyle for functions. This is really for personal taste, maybe for Java influence, but I really like functions and classes to look different.

If I had to argue about this, I would say that the difference is somewhat more than just aesthetic. This saves a little thought when you come across a temporary style design. On the contrary, you can argue that it doesn’t really matter if Foo(1,2,3) function call or not - if it is a constructor, then it acts just like a function that returns Foo by value in any case .

The convention also avoids the fiasco with function-with-that-and-ah-ah-ah-ah, which C ++ inherits, since C has a separate tag namespace:

 #include <iostream> struct Bar { int a; Bar() : a(0) {} Bar(int a) : a(a) {} }; struct Foo { Bar b; }; int Bar() { return 23; } int main() { Foo f; fb = Bar(); // outputs 23 std::cout << fba << "\n"; // This line doesn't compile. The function has hidden the class. // Bar b; } 

The bar, after all, is both a noun and a verb, so it can be reasonably defined as a class in one place and a function in another. Obviously, there are better ways to avoid collisions, such as using namespaces correctly. So, as I say, this is actually simply because I prefer the appearance of functions with lower initials, and not because it is actually necessary to distinguish them from classes.

+7
Nov 22 '09 at 2:54
source share

Unlike Java, C ++ does not have a “standard style”. The fairly large company I have ever worked for has its own C ++ coding style, and most open source projects also have their own styles. A few coding conventions you can look at:

It is interesting to note that C ++ coding standards often indicate which parts of the language to not use. For example, the Google C ++ Style Guide says: "We do not use C ++ exceptions." Almost everywhere I worked, certain parts of C ++ are forbidden. (In one place where I worked, it was said: "The program is in C, but with new and delete everything is fine"!)

+5
Nov 21 '09 at 18:35
source share

I think this is a matter of preference, although I prefer myFunction(...)

+4
Nov 21 '09 at 18:29
source share

As others have said, in C ++ there is no such thing. Having said that, I tend to use the style in which the standard library is written - K and R.

Also, see the Q&A section of Bjarne Stroustrup .

+4
Nov 21 '09 at 19:01
source share

Do as you like until you agree with your developer. Group. every few years changes to conventions ... (remmeber nIntVAr) ...

+3
Nov 21 '09 at 18:28
source share

There is not much "right" way for a language. This is a more personal preference or what is standard for your team. I usually use myFunction () when I make my own code. In addition, a style that you did not mention that you often see in C ++ is my_function () - without capital letters, underlining instead of spaces.

Indeed, this is simply dictated by the code in which you work. Or, if this is your own project, then your own personal preferences.

+3
Nov 21 '09 at 18:28
source share

It all depends on your correct definition. There are many ways to appreciate the coding style. Readability is important (for me). That is why I would use the my_function method to write function names and variable names.

+2
Nov 21 '09 at 18:34
source share



All Articles