Filenames should be all lowercase and can include underscores (_
) or dashes (-
). Follow the convention that your project uses. If there is no consistent local pattern to follow, prefer "_".
MyExcitingClass
,
MyExcitingEnum
.
The names of all types — classes, structs, typedefs, and enums — have the same naming convention. Type names should start with a capital letter and have a capital letter for each new word. No underscores.
Variable names are all lowercase, with underscores between words. Class member variables have trailing underscores. For instance: my_exciting_local_variable
,my_exciting_member_variable_
.
Common Variable names
For example:
string table_name; // OK - uses underscore. string tablename; // OK - all lowercase.
结尾加“_”
Struct Variables 同Common
Global Variables 前缀 “g_”
k
followed by mixed case:
kDaysInAWeek
.
All compile-time constants, whether they are declared locally, globally, or as part of a class, follow a slightly different naming convention from other variables.
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()
.
Regular Functions
Functions should start with a capital letter and have a capital letter for each new word. No underscores.
If your function crashes upon an error, you should append OrDie to the function name. This only applies to functions which could be used by production code and to errors that are reasonably likely to occur during normal operation.
AddTableEntry() DeleteUrl() OpenFileOrDie()
Accessors and Mutators
Accessors and mutators (get and set functions) should match the name of the variable they are getting and setting. This shows an excerpt of a class whose instance variable is num_entries_
.
class MyClass { public: ... int num_entries() const { return num_entries_; } void set_num_entries(int num_entries) { num_entries_ = num_entries; } private: int num_entries_; };
You may also use lowercase letters for other very short inlined functions. For example if a function were so cheap you would not cache the value if you were calling it in a loop, then lowercase naming would be acceptable.
Namespace names are all lower-case, and based on project names and possibly their directory structure: google_awesome_project
.
Enumerators should be named either like constants or like macros: either kEnumName
or ENUM_NAME
.