C++ Coding Standard - C++ 编程规范
C++ Coding Standard
https://users.ece.cmu.edu/~eno/coding/CppCodingStandard.html
Adapted from C++ Coding Standard
http://www.possibility.com/Cpp/CppCodingStandard.html
C Coding Standard
https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html
1. Contents
1. Resources- Take a Look!
2. Names
(important recommendations below)
Include Units in Names
Class Names
Class Library Names
Method Names
Class Attribute Names
Method Argument Names
C++ File Extensions
C Function Names
(other suggestions below)
Make Names Fit
Variable Names on the Stack
Pointer Variables
Reference Variables and Functions Returning References
Global Variables
Global Constants
Static Variables
Type Names
Enum Names
#define and Macro Names
3. Formatting
(important recommendations below)
Brace {} Policy
Parens () with Key Words and Functions Policy
A Line Should Not Exceed 78 Characters
If Then Else Formatting
switch Formatting
Use of goto,continue,break and ?:
(other suggestions below)
One Statement Per Line
Alignment of Declaration Blocks
4. Classes
(important recommendations below)
Naming Class Files
Class Layout
Order of public/protected/private?
What should go in public/protected/private?
Required Methods for a Class
Method Layout
Use of Namespaces
Use Header File Guards
(other suggestions below)
Different Accessor Styles
Initialize all Variables
Think About What Work to do in Constructors
Be Careful Throwing Exceptions in Destructors
5. Documentation
(important recommendations below)
Comments Should Tell a Story
Document Decisions
Use Headers
Make Gotchas Explicit
Commenting function declarations
(other suggestions below)
Include Statement Documentation
6. Complexity Management
Layering
Delegation
Minimize Dependencies with Abstract Base Classes
Liskov's Substitution Prinicple
Follow the Law of Demeter
Miscellaneous
Be Const Correct
Use Streams
Use #if Not #ifdef
Commenting Out Large Code Blocks
Don't Over Use Operators
Short Methods
No Magic Numbers
Error Return Check Policy
To Use Enums or Not to Use Enums
Macros
The Bull of Boolean Types
Usually Avoid Embedded Assignments
Mixing C and C++
7. Miscellaneous
No Data Definitions in Header Files
Make Functions Reentrant