Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this questionWhat's the convention for naming functions in C++?
I come from the Java environment so I usually name something 开发者_JAVA技巧like:
myFunction(...) {
}
I've seen mixed code in C++,
myFunction(....)
MyFunction(....)
Myfunction(....)
What's the correct way?
Also, is it the same for a class method as well as a non-class method?
Since C++11, you may want to use either snake_case
or camelCase
for function names.
This is because to make a class work as the range-expression in a range-based for-loop, you have to define functions called begin
and end
(case-sensitive) for that class.
Consequently, using e.g. PascalCase
for function names means you have to break the naming consistency in your project if you ever need to make a class work with the range-based for.
There isn't a 'correct way'. They're all syntactically correct, though there are some conventions. You could follow the Google style guide, although there are others out there.
From said guide:
Regular functions have mixed case; accessors and mutators match the name of the variable: MyExcitingFunction(), MyExcitingMethod(), my_exciting_member_variable(), set_my_exciting_member_variable().
Most code I've seen is camelCase
functions (lower case initial letter), and ProperCase/PascalCase
class names, and (most usually), snake_case
variables.
But, to be honest, this is all just guidance. The single most important thing is to be consistent across your code base. Pick what seems natural / works for you, and stick to it. If you're joining a project in progress, follow their standards.
The most common ones I see in production code are (in this order):
myFunctionName // lower camel case
MyFunctionName // upper camel case
my_function_name // K & R ?
I find the naming convention a programmer uses in C++ code usually has something to do with their programming background.
E.g. ex-java programmers tend to use lower camel case for functions
If you look at the standard libraries the pattern generally is my_function, but every person does seem to have their own way :-/
Personally, I prefer thisStyle
to ThisStyle
for functions. This is really for personal taste, probably Java-influenced, but I quite like functions and classes to look different.
If I had to argue for it, though, I'd say that the distinction is slightly more than just aesthetic. It saves a tiny bit of thought when you come across function-style construction of a temporary. Against that, you can argue that it doesn't actually matter whether Foo(1,2,3)
is a function call or not - if it is a constructor, then it acts exactly like a function returning a Foo by value anyway.
The convention also avoids the function-with-same-name-as-a-class-is-not-an-error fiasco that C++ inherits because 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;
f.b = Bar();
// outputs 23
std::cout << f.b.a << "\n";
// This line doesn't compile. The function has hidden the class.
// Bar b;
}
Bar is, after all, both a noun and a verb, so could reasonably be defined as a class in one place and a function in another. Obviously there are better ways to avoid the clash, such as proper use of namespaces. So as I say, really it's just because I prefer the look of functions with lower-case initials rather than because it's actually necessary to distinguish them from from classes.
Unlike Java, C++ doesn't have a "standard style". Pretty much very company I've ever worked at has its own C++ coding style, and most open source projects have their own styles too. A few coding conventions you might want to look at:
- GNU Coding Standards (mostly C, but mentions C++)
- Google C++ Style Guide
- C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
It's interesting to note that C++ coding standards often specify which parts of the language not to use. For example, the Google C++ Style Guide says "We do not use C++ exceptions". Almost everywhere I've worked has prohibited certain parts of C++. (One place I worked basically said, "program in C, but new
and delete
are okay"!)
I think its a matter of preference, although i prefer myFunction(...)
As others said, there is no such thing in C++. Having said that, I tend to use the style in which the standard library is written - K & R.
Also, see the FAQ entry by Bjarne Stroustrup.
Do as you wish, as long as your are consistent among your dev. group. every few years the conventions changes..... (remmeber nIntVAr)...
There isn't so much a 'correct' way for the language. It's more personal preference or what the standard is for your team. I usually use the myFunction() when I'm doing my own code. Also, a style you didn't mention that you will often see in C++ is my_function() - no caps, underscores instead of spaces.
Really it is just dictated by the code your working in. Or, if it's your own project, your own personal preference then.
It all depends on your definition of correct. There are many ways in which you can evaluate your coding style. Readability is an important one (for me). That is why I would use the my_function
way of writing function names and variable names.
精彩评论