C + + Naming conventions

Source: Internet
Author: User
Tags mixed

The code makes the program aesthetically pleasing, and I prefer to read her.

Refer to the Google Coding specification http://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/

File naming

General statement

File names are all lowercase and can contain underscores ( _ ) or hyphens ( - ), according to the contract of the project. If there is no agreement, then " _ " better.

Description

An example of acceptable file naming:

    • my_useful_class.cc
    • my-useful-class.cc
    • myusefulclass.cc
    • myusefulclass_test.cc_unittestand _regtest deprecated.

The C + + file ends with a .cc header file .h . The file that is specifically inserted into the text ends with the .inc header file self-contained.

Do not use /usr/include a file name that already exists under (Yang.y Note: The path of the compiler to search for the system header file), such as db.h .

You should always try to make the file name more explicit. http_server_logs.hIt's better than that logs.h . When you define a class, the file name usually appears in pairs, such as foo_bar.h and foo_bar.cc , corresponding to the class FooBar .

Inline functions must be placed in a .h file. If the inline function is shorter, it is placed directly in .h .

* * My previous approach was to capitalize the first letter of the word with the same class name, and I don't know if it's good to see the Arduino library code. Since Google has a complete coding specification, I'll write it according to his specifications.

Type naming

General statement

The first letter of each word of the type name is capitalized and does not contain underscores: MyExcitingClass MyExcitingEnum .

Description

All type naming-classes, structs, type definitions ( typedef ), enumerations, type template parameters-all use the same convention, starting with uppercase letters, with the first letter of each word capitalized and without underscores. For example:

Class and struct bodyClassUrltable{...ClassUrltabletester{... struct urltableproperties { ... //Type definition typedef HASH_MAP<urltableproperties *, string>  Propertiesmap;  Using alias using propertiesmap = hash_map<urltableproperties *,  String>;  Enum enum urltableerrors { ...            
* * The naming of classes is consistent with my previous practice, except that I do not know the type naming. (class name, struct name, type definition, alias, enum class name)
Variable naming

General statement

Variables (including function parameters) and data member names are all lowercase, and the words are concatenated with underscores. The member variable of the class ends with an underscore, but the struct is not used, such as: a_local_variable , a_struct_data_member a_class_data_member_ .

Description

Common variable naming

Example:

table_name;  //good-underline. tablename//good-all lowercase.  TableName//difference-Mixed case     
Class data members

Whether static or non-static, class data members can be the same as normal variables, but are underlined.

{  privatetable_name_tablename_Pool<tableinfoPool_//good.};  
struct-Body variables

Whether static or non-static, struct data members can be the same as normal variables without being underlined like a class:

{  namenum_entriespool<urltablepropertiesPool  ; };
* * Here static and non-static does not distinguish between feeling bad, do not know where to see the static S_ head, global g_ begins. Next time, fill up.
Constant naming

General statement

A constexpr variable that is declared or, const or whose value is always constant during the run of the program, is named with the word "K" and mixed in case. For example:

7;

Description

All variables that have a static storage type, such as static variables or global variables, see Storage types, should be named in this manner. For variables of other storage types, such as automatic variables, this rule is optional. If you do not use this rule, you will follow the general variable naming rules.

* * This refers to all variables with static storage types (such as static or global variables, see storage types ) should be named in this manner, with "K" "s" "G" beginning, mixed case.

function naming

General statement

Regular functions use case blending, and the value and value functions require matching with the variable name:, MyExcitingFunction() , MyExcitingMethod() my_exciting_member_variable() , set_my_exciting_member_variable() .

Description

In general, the first letter of each word of the function name is capitalized (that is, "Hump variable name" or "Pascal variable name"), without underlining. For acronyms, they are more likely to be capitalized as a single word (for example, writing StartRpc() rather than StartRPC() ).

Addtableentry()deleteurl()openfileordie(     )

(The same naming conventions apply to both class scope and namespace scope constants, because they are exposed as part of the API, so they should look like a function, because at this point they are actually an object, not a function.) The fact that it is a matter of fact is an insignificant implementation detail.

The name of the value and the SetPoint function is consistent with the variable. In general, their names correspond to the actual member variables, but they are not mandatory. For example int count() with void set_count(int count) .

* * I am seriously accustomed to the function of naming the first letter lowercase, this is still to keep it. The value and set value are consistent with the variable name get_count (), Set_count ().

namespace naming

General statement

Namespaces are named in lowercase letters. The name of the highest-level namespace depends on the project name. Be aware that there is a conflict between the names of nested namespaces and the names of common top-level namespaces.

The name of the top-level namespace should be the name of the project or the team that the code in that namespace belongs to. The code in the namespace should be stored in a folder or its subfolders that match the name of the namespace.

Note rules that do not use abbreviations as names also apply to namespaces. There is very little code in the namespace that involves the name of the namespace, so it is not necessary to use abbreviations in the namespace.

To avoid name collisions with a common top-level namespace, the nested namespaces do not occur. Because of the existence of name lookup rules, conflicts between namespaces are completely likely to cause compilation to fail. In particular, do not create nested std namespaces. It is recommended to use a more unique project identifier ( websearch::index , websearch::index_util ) rather than a common, easily conflicting name (for example websearch::util ).

For internal namespaces, be aware internal of conflicts between code that joins the same namespace (because internal maintainers often come from the same team and therefore often cause conflicts). In this case, use the file name to make the internal name unique (for example frobber.h , using the ).

* * namespace this doesn't work.
Enumeration naming

General statement

The name of the enumeration should be consistent with constants or macros: kEnumName or ENUM_NAME .

Description

Individual enumeration values should take precedence over the naming of constants. But the name of the macro method is also acceptable. The enumeration name UrlTableErrors (and AlternateUrlTableErrors ) is the type, so the case is mixed in the same way.

{    0kerroroutofmemorykerrormalformedinput,};  012,};         

Until January 2009, we have been recommending the use of macros to name enumeration values. Because of the naming conflict between the enumeration values and macros, there are a number of problems that directly result. As a result, the name of the constant style is preferred instead. The new code should use the constant style as preferentially as possible. But the old code doesn't have to switch to a constant style unless the macro style does produce a compile-time problem.

Macro naming

General statement

You're not going to use a macro, are you? If you must use, name it like this: MY_MACRO_THAT_SCARES_SMALL_CHILDREN .

Description

Refer to pre-processing macros; Macros should not normally be used. If it has to be used, its name is all capitalized as the enumeration name, using underscores:

#define ROUND (x) ... #define PI_ROUNDED 3.0
* * All CAPS Fall line split

C + + Naming conventions

Related Article

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.