1. General rules: Do not abbreviations at will;
2. Use uppercase letters and underscores (_) for macros and enumerations;
3. the variables (including class and struct member variables), files, namespaces, and access functions are all lowercase letters and underscores (_). The class member variables are underlined and ended with G _. 4. refer to existing or similar naming conventions ......
Naming Conventions
The most important consistency rule is naming management. The naming style can be used to directly determine the type, variable, function, constant, Macro, and so on without looking for object declarations, the pattern matching engine in our brains depends on these naming rules.
Naming rules are random, but consistency is more important than naming based on personal preferences. Therefore, no matter what you think, rules are always rules.
1. General naming rules (general naming Rules)
Function naming, variable naming, and file naming should be descriptive and should not be over-abbreviated. Types and variables should be nouns. function names can use the "imperative" verb.
How to name:
Give descriptive names as much as possible, and do not save space. It is more important for others to quickly understand your code. Good naming options:
Int num_errors; // good.
Int num_completed_connections; // good.
Ugly naming uses fuzzy abbreviations or random characters:
Int N; // bad-meaningless.
Int nerr; // bad-Ambiguous abbreviation.
Int n_comp_conns; // bad-Ambiguous abbreviation.
Type and variable names are generally nouns, such as fileopener and num_errors.
The function name is usually directive, such as openfile () and set_num_errors (). To access the function, you need to describe it in more detail and match it with the accessed variable.
Abbreviation:
Do not use abbreviations unless they are placed outside the project. For example:
// 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 ".
Do not omit the abbreviations of letters:
Int error_count; // good.int error_cnt; // bad.
2. File Names)
The file name must be in lowercase. It can contain underscores (_) or hyphens (-), as specified in the project.
Acceptable File naming:
My_useful_class.cc
My-useful-class.cc
Myusefulclass. CC
The C ++ file ends with. CC, And the header file ends with. h.
Do not use a file name that already exists in/usr/include (for UNIX, Linux, and other systems), such as DB. h.
Generally, make the file name clearer as much as possible. http_server_logs.h is better than logs. H. when defining a class, the file name usually appears in pairs, such as foo_bar.h and foo_bar.cc, and the corresponding class foobar.
Inline functions must be placed in. H files. If inline functions are short, they are placed directly in. h. If the code is long, you can put it in a file ending with-inL. h. For classes that contain a large number of Inline code, there can be 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.
Refer to Article 1-inL. h.
3. type names)
Type name: each word starts with an uppercase letter and does not contain underscores (_): myexcitingclass or myexcitingenum.
All types of names-class, struct, type definition (typedef), enumeration-use the same conventions, for example:
// Classes and structs
Class urltable {...
Class urltabletester {...
Struct urltableproperties {...
// Typedefs
Typedef hash_map <urltableproperties *, string> propertiesmap;
// Enums
Enum urltableerrors {...
4. variable naming (variable names)
The variable names are all lowercase, And the words are separated by the following strip. The member variables of the class end with an underscore, for example, my_exciting_local_variable and my_exciting_member_variable _.
Common variable name:
Example:
String table_name; // OK-uses underscore.
String tablename; // OK-all lowercase. String tablename; // bad-mixed case.
Class data member:
The data member of a struct can be the same as a common variable without being underlined like a class:
Struct urltableproperties {
String name;
Int num_entries;
}
For more information about struct and classes, see section 3 "struct vs. Class.
Global variables:
There are no special requirements for global variables. It is good to use less. You can use g _ or other labels that are easy to distinguish from local variables as prefixes.
5. Constant naming (constant names)
Add K: kdaysinaweek to the name.
All compilation times (whether local, global, or class) are slightly different from other variables. k is followed by words starting with an upper-case letter:
Const int kdaysinaweek = 7;
6. function names)
Regular Functions (), myexcitingmethod (), my_exciting_member_variable (), set_my_exciting_member_variable ().
Common functions:
The function name starts with an uppercase letter. Each word has an uppercase letter without an underscore:
Addtableentry ()
Deleteurl ()
Access function:
The access function must match the name of the accessed variable. Here we extract a class with the instance variable 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 _;
};
Other short inline function names can also use lower-case letters. For example, if you call a function in a loop, you do not even need to cache its value. The lower-case name is acceptable.
Note: The lower-case function name can be used in inline mode.
7. namespace names)
The namespace name is in all lowercase. Its name is based on the project name and directory structure: google_awesome_project.
For more information about namespaces and how to name them, see the second namespace.
8. enumerator names)
The enumerated values should all be in upper case, and the words should be underlined as follows: my_exciting_enum_value.
The enumerated name belongs to the type, so it is a combination of uppercase and lowercase: urltableerrors.
Enum urltableerrors {
OK = 0,
Error_out_of_memory,
Error_malformed_input,
};
9. Macro naming (macro names)
You are not planning to use macros, are you? For example, my_macro_that_scares_small_children.
For more information, see section 4 Pre-processing macros. Generally, macros are not used. If they are to be used, the names are all uppercase like enumeration, and underlines are used:
# Define round (x )...
# Define pi_rounded 3.0
My_exciting_enum_value
10. Naming rule exceptions (exceptions to naming Rules)
When naming an object similar to an existing C/C ++ object, you can refer to the existing naming conventions:
Bigopen ()
Function Name, refer to open ()
Uint
Typedef Type Definition
Bigpos
Struct or class, refer to POS
Sparse_hash_map
STL similar entities; refer to STL naming conventions
Longlong_max
Constant, similar to int_max
The naming conventions are much easier. The naming conventions are slightly casual while complying with code consistency and readability:
1. general rule: do not be abbreviated. If changelocalvalue is still rational when writing chglocval, it is too much to write modifyplayername into mdfplynm. In addition to the function name, it can be a verb, use clear and easy-to-understand terms for other naming rules;
2. Use uppercase letters and underscores (_) for macros and enumerations;
3. variables (including class and struct member variables), files, namespaces, and access functions are all lowercase letters and underscores (_). The class member variables are underlined and ended with G;
4. Common functions, types (including classes, struct, and enumeration types), constants, and so on are case-insensitive and do not contain underscores;
5. Refer to existing or similar naming conventions.
This article from programming entry Network: http://www.bianceng.cn/Programming/cplus/201009/19337.htm