Function names in C++: Capitalize or not? [closed]
Solution 1:
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.
Solution 2:
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().
Solution 3:
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.
Solution 4:
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