Naming Conventions for Google C ++ Programming

Source: Internet
Author: User
Tags uppercase letter

1. general rules: Do not abbreviations; 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, do not save space, so that others can quickly understand yourCodeMore importantly, 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: for exampleFileopener,Num_errors.

Function names are generally mandatory, suchOpenfile (),Set_num_errors ()To access the function, you need to describe it in more detail. It must be consistent with the accessed variable.

Abbreviations:

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

C ++ File. CCEnd with the header file. HEnd.

Do not use an existing/Usr/includeFile Name (for UNIX, Linux, and other systems), suchDB. h.

Generally, try to make the file name clearer,Http_server_logs.hRatioLogs. hBetter. when defining a class, the file name usually appears in pairs, as shown in figureFoo_bar.hAndFoo_bar.cc, Corresponding classFoobar.

Inline functions must be placed in. HFile, if the inline function is relatively short, put it directly. H. If the code is long, you can put it 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.

 

Refer to Article 1-InL. h file.

3. type names)

Type name: each word starts with an uppercase letter and does not contain underscores:Myexcitingclass,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)

All variable names are in lower case. The words are separated by strikethroughs, and the member variables of the class end with strikethroughs, as shown in figureMy_exciting_local_variable,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;
}

 

References for struct and ClassesStruct vs. Class.

Global Variables:

There are no special requirements for global variables. You can useG _Or other signs that are easy to distinguish from local variables are prefix.

5. Constant naming (constant names)

AddK:Kdaysinaweek.

All the compilation times (whether local, global, or class) are slightly different from other variables,KFollowed by words starting with an upper-case letter:

 
Const int kdaysinaweek = 7;

 

6. function names)

Regular Functions:Myexcitingfunction (),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 Functions:

The access function must match the name of the accessed variable. Here we extract a variable with an instance.Num_entries _Class:

 
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, and 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 section 2.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.

Enumeration names belong to the type, so they are case-insensitive: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.

Refer to Article 4Preprocessing macroGenerally, 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 Open ()
Uint
Typedef Type Definition
Bigpos
StructOr Class, refer Pos
Sparse_hash_map
STL similar entities; refer to STL naming conventions
Longlong_max
Constant, similar 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.