当前位置:编程学习 > C/C++ >>

CodeStyleConventions 代码风格约定

英文原文:
GENERAL
-------
 
 
Use real tabs that equal 4 spaces.
 
 
 
 
Use typically trailing braces everywhere (if, else, functions, structures, typedefs, class definitions, etc.)
 
 
if ( x ) {
}
 
 
 
 
The else statement starts on the same line as the last closing brace.
 
 
if ( x ) {
} else {
}
 
 
Pad parenthesized expressions with spaces
 
 
if ( x ) {
}
 
 
Instead of 
 
 
if (x) {
}
 
 
And
 
 
x = ( y * 0.5f );
 
 
Instead of
 
 
x = (y * 0.5f);
 
 
Use precision specification for floating point values unless there is an explicit need for a double.
 
 
float f = 0.5f;
 
 
Instead of
 
 
float f = 0.5;
 
 
And
 
 
float f = 1.0f;
 
 
Instead of
 
 
float f = 1.f;
 
 
 
 
Function names start with an upper case:
 
 
void Function( void );
 
 
 
 
In multi-word function names each word starts with an upper case:
 
 
void ThisFunctionDoesSomething( void );
 
 
 
 
The standard header for functions is:
 
 
/*
====================
FunctionName
 
 
  Description
====================
*/
 
 
 
 
Variable names start with a lower case character.
 
 
float x;
 
 
 
 
In multi-word variable names the first word starts with a lower case character and each successive word starts with an upper case.
 
 
float maxDistanceFromPlane;
 
 
 
 
Typedef names use the same naming convention as variables, however they always end with "_t".
 
 
typedef int fileHandle_t;
 
 
 
 
Struct names use the same naming convention as variables, however they always end with "_t".
 
 
struct renderEntity_t;
 
 
 
 
Enum names use the same naming convention as variables, however they always end with  "_t". The enum constants use all upper case characters. Multiple words are separated with an underscore.
 
 
enum contact_t {
CONTACT_NONE,
CONTACT_EDGE,
CONTACT_MODELVERTEX,
CONTACT_TRMVERTEX
};
 
 
 
 
Names of recursive functions end with "_r"
 
 
void WalkBSP_r( int node );
 
 
 
 
Defined names use all upper case characters. Multiple words are separated with an underscore.
 
 
#define SIDE_FRONT 0
 
 
 
 
Use ‘const’ as much as possible.
 
 
Use:
 
 
const int *p; // pointer to const int
int * const p; // const pointer to int
const int * const p; // const pointer to const int
 
 
Don’t use:
 
 
int const *p;
 
 
 
 
CLASSES
-------
 
 
The standard header for a class is:
 
 
/*
===============================================================================
 
 
Description
 
 
===============================================================================
*/
 
 
Class names start with "id" and each successive word starts with an upper case.
 
 
class idVec3;
 
 
 
 
Class variables have the same naming convention as variables.
 
 
class idVec3 {
float x;
float y;
float z;
}
 
 
 
 
Class methods have the same naming convention as functions.
 
 
class idVec3 {
float Length( void ) const;
}
 
 
Indent the names of class variables and class methods to make nice columns. The variable type or method return type is in the first column and the variable name or method name is in the second column.
 
 
class idVec3 {
float x;
float y;
float z;
float Length( void ) const;
const float *  ToFloatPtr( void ) const;
}
 
 
The * of the pointer is in the first column because it improves readability when considered part of the type.
 
 
 
 
Ording of class variables and methods should be as follows:
 
 
1. list of friend classes
2. public variables
3. public methods
4. protected variables
5. protected methods
6. private variables
7. private methods
 
 
This allows the public inte易做图ce to be easily found at the beginning of the class.
 
 
 
 
Always make class methods ‘const’ when they do not modify any class variables.
 
 
Avoid use of ‘const_cast’.  When object is needed to be modified, but only const versions are accessible, create a function that clearly gives an editable version of the object.  This keeps the control of the ‘const-ness’ in the hands of the object and not the user.
 
 
Return ‘const’ objects unless the general usage of
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,