Self-use specifications, reference from Google, Huawei and Microsoft.
?
The most important consistency rule is named management, and the naming style directly determines that named entities are: types, variables, functions, constants, macros, and so on, without looking up entity declarations, and the pattern matching engine in our brains relies on these naming conventions.
Naming rules are arbitrary, but consistency is more important than naming according to personal preference, so no matter what you think, the rules are always the rules.
<br>
1. Universal Naming conventions
?
1.1 The name of the identifier to be clear, clear, have a definite meaning, while using the complete word or basic understandable abbreviations, to avoid misunderstanding.
Description: Give as descriptive a name as possible, and don't save space and make it more important for others to quickly understand your code.
A good naming example:
int num_errors; // Good.int num_completed_connections; // Good.
Bad naming, using vague abbreviations or random characters:
int n; // Bad - meaningless.int nerr; // Bad - ambiguous abbreviation.int n_comp_conns; // Bad - ambiguous abbreviation.
1.2 In addition to the common general abbreviations, do not use the word abbreviation, not the use of Hanyu Pinyin.
Note: Shorter words can be shortened by removing the "vowel", a longer word may be abbreviated to the first few letters of the word, some words are generally recognized abbreviations, the abbreviations of commonly used words must be unified. The abbreviation of the word in the agreement is consistent with the agreement. Specific abbreviations used for a system should be uniformly explained in comments or somewhere.
Examples of some common examples that can be abbreviated:
Full Name |
Abbreviations |
Full Name |
Abbreviations |
Full Name |
Abbreviations |
Argument |
Arg |
Maximum |
Max |
Initialize |
Init |
Buffer |
Buff |
Message |
Msg |
Temp |
Tmp |
Clock |
Class |
Minimum |
Min |
Increment |
Inc |
Command |
Cmd |
Parameter |
Para |
Synchronize |
Sync |
Compare |
Cmp |
Previous |
Prev |
Hexadecimal |
Hex |
Configuration |
Cfg |
Register |
Reg |
Statistic |
Stat |
Device |
Dev |
Semaphore |
Sem |
Error |
Err |
1.3 Use the correct antonyms group to name a variable with mutually exclusive meaning or a function of the opposite action.
Example |
Example |
Example |
Add/remove |
Begin/end |
Create/destroy |
Insert/delete |
First/last |
Get/release |
Increment/decrement |
Put/get |
Add/delete |
Lock/unlock |
Open/close |
Min/max |
Old/new |
Start/stop |
Next/previous |
Source/target |
Show/hide |
Send/receive |
Source/destination |
Copy/paste |
Up/down |
? ?
1.4 Try to avoid numeric numbering in the name unless it is logically required.
"????" Example: The following name makes people wonder.
#define EXAMPLE_0_TEST_#define EXAMPLE_1_TEST_
Should be changed to a meaningful word name:
#define Example_unit_test_#define example_assert_test_
? A
1.5 identifier should not be prefixed with the name of a module, project, product, or department before it is added.
Description: Many of the existing code has been accustomed to add the module name in the file name, this method is similar to the Hungarian nomenclature, resulting in the file name is unreadable, and brings the following problems:
1 The first thing you see is the module name, not the real file function, hinder reading;
2) file name is too long;
3) file name and module bindings are not conducive to maintenance and porting. If the foo.c is refactored, move from module A to Module B, and if the module name is in foo.c, change the file name from A_MODULE_FOO.C to B_MODULE_FOO.C.
? The identifier naming style for the
1.6 platform/driver adaptation code remains the same as the platform/driver.
??????: related to the purchase of chips and ancillary drivers, this part of the code changes (including the product to do the new code for adaptation), should maintain the original style.
?
1.7 refactoring/Modifying part of the code should remain consistent with the naming style of the original code.
???? Note: Continuing to write code based on the existing style of the source code is conducive to maintaining overall consistency.
??
??
2. File name
because different systems have different file name case processing (such as MS DOS, Windows systems are case insensitive, but Linux systems are differentiated), filenames are all lowercase and can contain underscores (_) or short-term (-), according to the project agreement.
Acceptable file names:
my_useful_class.cpp
My-useful-class.cpp
Myusefulclass.cpp
C + + files end With. cpp and header files end with. H.
Do not use file names that already exist under/usr/include (for systems such as UNIX, Linux, etc.), such as Db.h.
Usually, as far as possible to make the file name more explicit, Http_server_logs.h is better than logs.h, define the class when the file name is generally paired, such as Foo_bar.h and Foo_bar.cpp, corresponding class FooBar. The
inline function must be placed in the. h file, and if the inline function is shorter, it is placed directly in the. h. If your code is long, you can put it in a file that ends in-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.
?
?
3. Type naming
Type name each word starts with an uppercase letter and does not contain underscores: Myexcitingclass, Myexcitingenum.
All types are named, including classes, structs, type definitions (typedef), and enumerations that use the same conventions, for example:
// classes and structsclass UrlTable { class UrlTableTester {struct UrlTableProperties { // typedefstypedef hash_map<UrlTableProperties *, string> PropertiesMap;// enumsenum UrlTableErrors {
?
?
4. Variable naming
???? Use UNIX style. Variable names are lowercase, the words are underlined, and the member variables of the class end with an underscore, such as
My_exciting_local_variable, My_exciting_member_variable_.
???? Use nouns or adjectives + nouns to name variables.
??
4.1 Common variable naming
???? Example:
string table_name; // OK - uses underscore.string tablename; // OK - all lowercase.string tableName; // Bad - mixed case.
Class 4.2 Data members
???? A struct's data member can be the same as a normal variable without being underlined like a class:
struct UrlTableProperties { string name; int num_entries;}
?
4.3 Global variables and static variables
???? Global variables are prefixed with g_.
???? Static variables are prefixed with s_.
???? Description: Add g_ prefix or s_ prefix for the following reasons:
???? First, global variables are dangerous, making global variables more visible through prefixes, prompting developers to be more cautious about using these variables.
???? Second, fundamentally, you should try not to use global variables, adding g_ and s_ prefixes, which makes the names of global variables look ugly, prompting developers to use as few global variables as possible.
?
4.4 Disables the use of single-byte named variables, but allows you to define I, J, and K as local loop variables.
?
4.5 Prohibition of the use of Hungarian nomenclature.
???? Description: Variable naming requires a description of the meaning of the variable, not the type of the variable. Adding a type description before the variable is named reduces the readability of the variable, and the more troubling question is that if you modify the type definition of the variable, all the places where the variable is used need to be modified.
???? The Hungarian nomenclature originates from Microsoft, but is used by many people baseless assertion. And now even Microsoft is no longer recommending the use of Hungarian nomenclature. Has always been a major criticism of the Hungarian nomenclature, which led to the variable name is difficult to read, which and the guiding ideology of this code is also a conflict, so this specification deliberately stressed that the name of the variable should not use the Hungarian nomenclature, but should be thought to make the variable named a meaningful word or phrase, convenient code reading.
??
??
5. Constant naming
???? Add k before the name, for example: Kdaysinaweek.
???? All compile-time constants (whether local, global, or class) are slightly different from other variables, and K is followed by a word that begins with an uppercase letter:
const int kDaysInAWeek = 7;
?
?
6. Function naming
???? The function name should be named after the function to perform the action, generally using a verb or verb + noun structure.
???? Example: Find the current directory of the current process
DWORD GetCurrentDirectory( DWORD BufferLength, LPTSTR Buffer );
In addition to the prefix, the function pointer is named according to the function's naming convention.
Common functions (regular functions, which are relative to special functions such as access functions) are mixed in case, and access functions (accessors and mutators) require matching with variable names: myexcitingfunction (), Myexcitingmethod (), my_exciting_member_variable (), set_my_exciting_member_variable ().
?
6.1 Common functions
???? The function name starts with an uppercase letter, with each word capitalized and without an underscore:
???? Addtableentry ()
???? DeleteUrl ()
?
6.2 Access functions
???? The access function is matched to the variable name of the access, which extracts a class that has 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 lowercase letters, for example, calling such a function in a loop does not even need to cache its value, and a lowercase name can be accepted.
?
?
7. Namespaces
The name of the namespace is all lowercase and is named based on the project name and directory structure: Google_awesome_project.
?
?
8. Enumeration naming
???? The enumeration values should all be uppercase and the words are underlined: my_exciting_enum_value.
???? Enumeration names are of type, so mixed case: Urltableerrors.
enum UrlTableErrors { OK = 0, ERROR_OUT_OF_MEMORY, ERROR_MALFORMED_INPUT,};
?
?
9. Macro naming
???? Use all uppercase letters to name the words with an underscore "_" between them. For example:
???? My_macro_that_scares_small_children\par
???? In addition to special identity definitions such as header files or compile switches, macro definitions cannot use the underscore "_" to begin and end.
?
(not to be continued)
2. Naming conventions I use for writing C + + codes