"C ++ Coding Specification" revision instructions (2)

zhaozj2021-02-08  463

"C Coding Specification" revision

Text P26 principle 2.3 less floating point unless you must

2.3.1 example

INT baudrate = 9600;

Int Symbolsin15msec;

// use floating point numbers to avoid?

change to

2.3.1 example

INT baudrate = 9600;

Int Symbolsin15msec;

// use floating point types to avoid?

Text P26 principle 2.3 less floating point unless you must

2.3.2 reasons

......

Abnormal treatment of floating point numbers complex (such as overflow, overflowing).

......

change to

2.3.2 reasons

......

Exceptions of floating point types complex (such as overflow, overflowing).

......

Text P26 Principle 2.4 Use the complex syntax of the program with TypeDef

2.4.1 example

// Simplify function pointer with TypeDef

Typedef int (* CallbackFuncptr_t) (INT Parameter);

change to

2.4.1 example

// Simplify function pointer with TypeDef

TypedEf Int (* CallbackFunctionPtr_t) (INT Parameter);

Text P26 Principle 2.4 Use the complex syntax of the program with TypeDef

2.4.3 Reference of Quantitative Analysis

The syntax containing more than 4 independent elements should be considered a complex syntax, such as the function pointer definition in the above example, is not 5 of the number of independent elements of TypeDef.

change to

2.4.3 Reference of Quantitative Analysis

The syntax containing more than 4 independent elements should be considered a complex syntax, such as the function pointer definition in the above example, not the number of independent elements of Typedef is 5 (int, *, callbackfunctionptr_t, int, parameter).

Text P28

2.7.2 reasons

......

Reduce anonymous namespace-class variables, constants, macros, and functions.

change to

Compliance with the Principles 1.8 Reduce Global Name Space Degree.

Text P30

Principle 3.1 must be used first defined

3.1.1 Description

C must do this (otherwise compiling through). The C procedure has no mandatory requirements, but it should also be provided first, then use the function.

3.1.2 Cause

First define that the compiler can check and find errors when compiling (rather than waiting for connection or runtime).

change to

Principle 3.1 Function must be used first declaration

3.1.1 Description

C must do this (otherwise compiling through). The C procedure has no mandatory requirements, but it should also be provided first, then use the function.

3.1.2 Cause

The first declaration allows the compiler to check and find errors when compiling (rather than waiting for connection or runtime).

Text P40 Principle 3.17 When the function returns a reference or pointer, use text to describe its validity period

3.17.1 Description

The validity period is the period of the guidance or pointer to effectively find the object. The text description is preferably in the header file where the function is located (it is directly directly).

change to

3.17.1 Description

The validity period is the time period of the target to effectively find the target. The text description is preferably in the header file where the function is located (it is directly directly).

Text P41

3.18.2 reasons

......

Returns a reference (constant) reference or pointer is still acceptable.

change to

For reference or pointers that can return read-only (constant), see "Principles 8.3 members can be exposed to constant forms".

Text P41 ~ 42

3.20.1 Description

If we can define a virtual function to define a virtual function, then the subclass can inherit the friend's interface without repeating the declaration of friend ...

change to

If we can define a virtual function to define a virtual function, then the subclass can inherit the function without repeating the claimed friend ... Text P45 Principle 4.2 members should be private (private)

4.2.2 Cause

Worse, any modification of the class will affect the code using this class because these code is entitled to directly access (modified) class members.

change to

Worse, any modification of the class can affect the code using this class because these code is entitled to directly access (modified) class members.

Text P47 Principle 4.5 Reduce the coupling between the class

4.5.2

Often see this example: There are many different types of objects in a large function module. In order to work together, how much each type of object has a pointer or reference, causing me, I have you, I will lead to it. One pot porridge, no one can ink anyone.

change to

4.5.2

Often see this example: There are many different types of objects in a large feature module. In order to work together, there are many pointers or references that have other objects inside each type of object, causing me in you, I have you, and finally Leading a pot of porridge, no one can ink anyone.

Text P50 Principle 4.9 Avoid providing access functions for each class member

the reason

......

Focus on attributes is inefficient in multithreaded environments. If you focus on behavior, once a behavior will modify a series of related properties and only the last lock (in a member function / behavior); and focus on attributes, you have to change the last lock for each modification (at each class member) Provide access functions because multiple threads should simultaneously call access functions (to modify the same attribute).

change to

......

Focus on attributes is inefficient in multithreaded environments. If you focus on behavior, once a behavior will modify a series of related properties and only the last lock (in a member function); and focus on attributes, you have to modify a property last lock (access to each class member) In the function), because multiple threads are prevented simultaneously from simultaneously calling the access function (to modify the same attribute).

Text P54

Principle 4.16 Reduce the number of anonymous namespace classes with nested methods

4.16.1 Description

You don't need to put all kinds of definitions in anonymous namespace. The class nested in other classes is not an anonymous namespace.

change to

Principle 4.16 Reduce the number of global namespaces with nested methods

4.16.1 Description

You don't need to put all kinds of definitions in the global namespace. The class nested in other classes is not a global namespace.

Text p55

Chapter 5 (object-oriented) inheritance

Inheritance is a basic feature of object-oriented language. This is a powerful feature, but it must be used, otherwise it does not play its characteristics, and even the opposite.

change to

Inheritance is a basic feature of object-oriented language. The function is strong, but the side effects of "breaking the package" are fresh, so they must be used, otherwise it does not play its characteristics, even suitable for it.

Text P55 Principle 5.2 About "Have" and "by ...

5.2.1 Description

(A class) contains (another class) means "there is a" or "by ... "Private Inheritance" means "realized by ...".

change to

5.2.1 Description

(A class) contains (another class) means "there is a" or "by ... "Private Inheritance" means "implementation" (and "containing" similar; completely different from "public inheritance").

Text P59

5.5.2 reasons

......

Similar to this, the number of the same layer can not be too much, otherwise it is necessary to add a parent class to do some (new) abstraction, thereby reducing the number of the same level (this is one Balanced art).

change to

Similarly, the number of subclasses derived from the same parent class can not be too much (family planning?), Otherwise it should be considered whether to add a parent class, through some extent (new) abstraction, reduce the same level The number of classes (this is a balanced art).

Text P59

Principle 5.6 Classes in inheriting the non-leaven nodes on the tree should be a virtual base class

5.6.1 example

If you have two (non-virtual) classes C1 and C2, and you want C2 to derive in C1, as shown on the left side of Figure 5-1, you should add a virtual base class A, and the C1 and C2 are derived from A, As shown in Figure 5-1.

change to

Principle 5.6 Classes of non-leaf nodes on the tree should be abstract base class

5.6.1 example

If you have two (non-abstract) classes C1 and C2, and I hope that C2 is derived from C1, as shown on the left side of Figure 5-1, it should be added to abstract base classes a, and C1 and C2 are Delicate from A, as shown in Figure 5-1.

Text P60

......

Further, since the C class encapsulates the specific implementation, only the interface is exit, so the interface of the C1 should be C2 most want to inherit. In other words, as long as C2 inherits the interface of C1, the object of C2 can be seen as an activity that C1 is involved in any C1 object. It can be seen that the interface itself (rather than a specific implementation) is the full essential condition of participating activities, which is why A is usually a virtual base class.

Let us change one angle, a big disadvantage inheritance is to break the package, that is, exposes the realization details of the base class to the derived class. The result is that the modification of the base class will not guarantee that it does not affect the derivation class, that is, the base class and derived class is a tight coupling relationship (see the coupling between the principle 4.5 reducing the class "), unless the group Class is purely (no detail).

In addition, in order to prevent the various problems brought by multiple inheritance (see "Principle 5.14 cautious with multiple inheritance"), it is also recommended that the amount of non-leaven nodes on the tree is not a member of the pure viable basis (similar to the Interface in Java). ).

change to

Further, since the C class encapsulates the specific implementation, only the interface is exit, so the interface of the C1 should be C2 most want to inherit. In other words, as long as C2 inherits the interface of C1, the object of C2 can be seen as an activity that C1 is involved in any C1 object. It can be seen that the interface itself (instead of implementation) is the full essential condition of participating activities, which is why A is usually only a complete abstract base class (similar to the Interface in Java) only defined the interface.

Another reason for the fully abstract base class: inheriting a large defect, it is to break the package, that is, exposes the details of the base class to the derived class. The result is that the modification of the base class will not guarantee that it does not affect the derivation class, that is, the base class and derived class is a tight coupling relationship (see the coupling between the principle 4.5 reducing the class "), unless the group The class is not completely detailed.

Third causes of complete abstract bases: In order to prevent the various problems brought by multiple inheritance (see "Principle 5.14 Careless Use Multiple Inheritance"), it is best not to be members of the non-leaven node on the tree. Text P73

6.8.3 Cause

Prevent it from using this pointer again.

change to

Prevent it from using this pointer again (using null will immediately cause system errors, better).

Text P85 Principle 7.19 Set the initial value of the cycle index near the cycle point

7.19.1 example

Suppose the loop queue subscripts from 0 to MAX-1 (maximum number of elements that MAX can accommodate the queue), the team should be located at MAX-3.

7.19.2 reasons

The most often problematic in the loop index is near the cycle point, and the team can be placed here soon.

The problem often appears in: Inserting element allows the subscript from MAX-1 back to 0, deleting elements, so that the subscript is returned from 0 to MAX-1, inserts the first one or two elements, and deletes the last two elements. Selecting MAX-3 can cover these situations in a breath.

change to

7.19.1 example

Suppose the cyclic queue subscripts from 0 to MAX-1 (max) the number of elements that can be accommodated for the queue), the team first travel should be located at MAX-3.

7.19.2 reasons

The most often problematic place in the loop index is near the cycle point, and the team's first jam can be discovered very often.

The problem often appears in: Inserting the element, allows the cursor from MAX-1 back to 0, deleting elements, so that the cursor is wrapped back to MAX-1, insert the first two elements, and delete the last two elements. Selecting MAX-3 can cover these situations in a breath.

Text p89

Principle 8.1 Meaning on constant modifiers

example

Char * p = "Hello."; // The pointer is not a constant, and the pointer points to it is not a constant.

const char * p = "Hello."; / / The pointer is not a constant, the pointer points to constants

Char * const p = "Hello."; // The pointer is constant, the pointer points to not constant

const char * const p = "hello"; / / pointer is constant, the pointer points to the constant

change to

Principle 8.1 Meaning on constant modifiers

8.1.1

Char * p = "Hello."; // The pointer is not a constant, and the pointer points to it is not a constant.

Char const * p = "Hello."; // The pointer is not a constant, the pointer points to constants

Char * const p = "Hello."; // The pointer is constant, the pointer points to not constant

Char const * const p = "hello"; // The pointer is constant, the pointer points to the constant

8.1.2 Description

"Char * p" means: p is a pointer; it points to the character type.

"Char const * p" means: p is a pointer; it points to a constant; this constant is a character type.

"Char * const p" means: p is a constant; it is a pointer constant; the constant pointer points to the character type.

"Char const * const p" means: p is a constant; it is a pointer constant; the constant pointer points to a constant; the constant that is pointed is character type.

Text P89 ~ 90

8.2.3 Cause

The compiler will always remember this. If any modifications to blockcopy () in the future, it will cause compilation errors to compile errors, which prompts the successor to follow the original design.

change to

The compiler will always remember this. If any modification of the BlockCopy () function body is written to the PSRC in the future, it will cause compilation errors to prompt the successor of the function to follow the original design. Text P92

8.5.2 reasons

The members of the program state change is not an absolute constant member function (not in line with most people's expectations on constant members).

change to

The members of the program state change is not an absolute constant member function. Although not necessarily violate C grammar, there is a problem with the semantics and does not meet the expectations of most people on constant members.

Text p123 principle 15.4 Do not end macro definition with a semicolon

example

/ *

* With the end of the semicolon, the result is caused by the second statement below.

* "Channel = CHANNEL_MAX; -1;", compiling errors

* /

change to

example

/ * ----------------------------------------

* With the end of the semicolon, the result is caused by the second statement below.

* "Channel = 16; -1;", compile errors

----------------------------------------- * /

Text p128

15.11.2 Cause

......

For macros that have to appear in public header files (such as preventing header files from being resented multiple references), add prefixed to reduce conflicts, see "Principle 1.7 About Anonymous Nattellation Space Level Identifies".

change to

......

For macros that have to appear in public header files (such as macro to prevent head files from being referenced), add prefixed to reduce conflicts, see "Principles 1.7 About global namespace level identifier".

Text P132 Principle 16.7 Especially when calm analyzes

the reason

The C exception handling mechanism provides that the program will be terminated if an abnormality occurs during the abnormal processing.

change to

The C exception handling mechanism provides that if an exception occurs during the abnormality processing (in the Catch statement block), the program will be terminated.

Text P142

17.15.2

// {} occupy a row alone, and the IF statement is indensed

change to

17.15.2

// {} occupy a row alone and the IF / for statement is indented

Text P146 Principle 17.20 provides default branches for all Switch statements

the reason

... This, once new branches have been added in the future, such as an enumeration adds a new value), don't worry if you will miss the relevant Switch statement.

change to

the reason

...... This way, once new branches have increased (for example, enumeration add new value), don't worry if you miss the relevant Switch statement (I miss the program to exit, you can discover).

Text P148

17.24.2 Cause

The compiler scans from the right left when parsing the variable definition, and the programmer is also used to this way. As shown in the above example, PNAME is first a pointer, followed by pointing to a constant, the third constant is a character type. Carefully taste these modifications, you will find that they have priority, the higher the priority, the more you want to put on the right (next to variable name).

change to

17.24.2 Cause

The compiler is scanned by the variable name when parsing the variable definition, and the programmer is also used to this way. As shown in the above example, PNAME is first a pointer, followed by pointing to a constant, the third constant is a character type. Carefully taste these modifications, you will find that they have priority, the higher the priority, the more it takes to be the variable name. (Can also refer to "Principle 8.1 About constant modifier")

Text P160

Principle 18.12 distinguishing "strategic" annotation and "tactical" notes

18.12.1 Description

Note "Strategic" and "Tactics" two categories:

"Strategic" annotation is used to illustrate a piece of code, so before the code is placed. Usually, such annotations are longer (more than one line), it is recommended to use / ** / style.

"Tactical" annotation is used to illustrate a code, usually after the code (notes at the end). It is recommended to use // style.

change to

Principle 18.12 Differential notes and untrue comments

18.12.1 Description

Note Segmentation and single lines:

Paragraph annotation is used to illustrate a piece of code, so before this code is placed. Usually, such annotations are longer (more than one line), it is recommended to use / ** / style.

Single-line annotations are used to illustrate a code, usually after the code (end comments). It is recommended to use // style.

Paragraph Note With the horizontal line (see example or the large section of the appendix), the purpose is to distinguish between code and annotations, which is conducive to code and annotation. (In this case, you will be able to do it yourself.)

Text P161 Principle 18.15 Reduce Unnecessary Separate Note

the reason

......

Class definitions and comments before the function (strategic) do not interrupt the code because they are outside a complete logic segment. Thereby, it can also be used to try the inch of the principle.

change to

the reason

......

Comments before class definitions and functions (paragraph annotation) do not interrupt code because they are outside a complete logic segment. Thereby, it can also be used to try the inch of the principle.

Text p172

20.1.4 Named way for this type of macro

......

See "Principle 1.7 About Anonymous Nature Space Level Identifier".

change to

......

See "Principle 1.7 for the prefix of global namespace level identifier".

Text P174 Principle 20.7 Put the function library in a separate directory

20.7.1

#include "MYLIBRARY / ZERRLOG.HPP"

change to

20.7.1

#include "z_library / zerrlog.hpp"

Text P208

24.15.1 Description

......

But some compilers will meet the classes that meet one of the following conditions as non-normalized categories:

......

There is a virtual base class.

change to

24.15.1 Description

......

But some compilers will meet the classes that meet one of the following conditions as non-normalized categories:

......

Abstract base class.

Text P213

25.1.2 Cause

Reduce naming conflicts:

For details, see "Principle 1.8 Reduce Anonymous Name Space Level Identifier".

change to

25.1.2 Cause

Reduce naming conflicts:

For details, see "Principles 1.8 Reduce Global Name Space Level Identifiers".

P220 appendix

/ *

* Constructor:

* Direct Initialization.

* /

Z_smartptr_t (

Pointee_t * ppointee,

Z_ABSTRACTFAACTORY_T * PFactory = NULL (2) (3)

);

change to

/ * ------------------------

* Constructor:

* Direct Initialization.

------------------------- * /

EXPLICIT Z_SMARTPTR_T (

Pointee_t * ppointee,

Z_ABSTRACTFAACTORY_T * PFactory = NULL (2) (3)

);

P222 appendix

// program notes: -multi-thread safe = false:

// this is Because these Kind of classes Is Just Like A // Dumb Pointer Which Need To Be Protected Outside.

change to

// program notes: -multi-thread safe = false:

// this is Because this Kind of classes is Just Like Dumb

// Pointers Which Need to be protected outside.

转载请注明原文地址:https://www.9cbs.com/read-253.html

New Post(0)