General naming rules:
The function name, variable name, and file name should be self-described, and abbreviations should be avoided. Types and variables should use nouns, and functions should contain verbs.
Int num_errors; // good.
Int num_completed_connections; // good.
Int N; // bad-meaningless.
Int nerr; // bad-Ambiguous abbreviation.
Int n_comp_conns; // bad-Ambiguous abbreviation.
Avoid abbreviations unless they are very common outside your project.
// Good
// These show proper names with no abbreviations.
Int num_dns_connections; // most people know what "DNS" stands.
Int price_count_reader; // OK, price count. Makes sense.
// Bad!
// Abbreviations can be confusing or ambiguous outside a small group.
Int wgc_connections; // only your group knows what this stands.
Int pc_reader; // lots of things can be abbreviated "PC ".
Int error_count; // good.
Int error_cnt; // bad.
File Name:
The file name must contain lowercase letters, underscores (_), and hyphens (-). Underline is recommended.
The following are acceptable file names:
My_useful_class.cc
My-useful-class.cc
Myusefulclass. CC
Myusefulclass_test.cc // _ unittest and _ regtest are deprecated.
Inline functions must be included in. H files.
If your inline function is small, you should write it directly to the. h file.
If your inline functions include a certain amount of code, they should be written to a third-party file ending with-inL. h.
If your class has many inline functions, your class should have three files:
Url_table.h // The class declaration.
Url_table.cc // The class definition.
Url_table-inl.h // inline functions that include lots of code.
Type name:
Type names (including: Class, struct, type definition, enumeration) start with an uppercase letter and each new word starts with an uppercase letter, excluding underscores.
// Classes and structs
Class urltable {...
Class urltabletester {...
Struct urltableproperties {...
// Typedefs
Typedef hash_map <urltableproperties *, string> propertiesmap;
// Enums
Enum urltableerrors {...
Variable name:
Variable names only contain lower-case letters and are underlined to separate each word. The class member variables should end with an underscore.
Common variable name:
String table_name; // OK-uses underscore.
String tablename; // OK-all lowercase.
String tablename; // bad-mixed case.
Class member variables:
String table_name _; // OK-underscore at end.
String tablename _; // OK.
Struct member variable: to distinguish it from a class member variable, the struct member variable does not end with an underscore.
Struct urltableproperties {
String name;
Int num_entries;
}
Global variable: The name is the same as that of a common variable, with G _ as the prefix.
Constant Name:
Use the first letter of each word after K in upper case
Const int kdaysinaweek = 7;
Function Name:
The common function name uses the upper-case method of each word. The function used to access class member variables matches the class member variable name: myexcitingfunction (), myexcitingmethod (), my_exciting_member_variable (),
Set_my_exciting_member_variable ().
Common Function Name:
Addtableentry ()
Deleteurl ()
Openfileordie ()
Function Name of the access variable:
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 _;
};
Namespace:
The namespace must be a lowercase letter based on the project name and storage path.
Enumeration Name:
Enumeration should use the same naming method for constants or macro definitions, that is, kenumname or enum_name
Enum urltableerrors {
KOK = 0,
Kerroroutofmemory,
Kerrormalformedinput,
};
Enum alternateurltableerrors {
OK = 0,
Out_of_memory = 1,
Malformed_input = 2,
};
Macro name:
Generally, macros should not be used. If you really need it, you must name it in uppercase and underline.
# Define round (x )...
# Define pi_rounded 3.0