CodeStyleConventions 代码风格约定(三)
.cpp
Winding.h
When a class spans across multiple files these files have a name that starts with the name of the class without "id", followed by an underscore and a subsection name.
class idRenderWorld;
files:
RenderWorld_load.cpp
RenderWorld_demo.cpp
RenderWorld_portals.cpp
When a class is a public virtual interface to a subsystem the public interface is implemented in a header file with the name of the class without "id". The definition of the class that implements the subsystem is placed in a header file with the name of the class without "id" and ends with "_local.h". The implementation of the subsystem is placed in a cpp file with the name of the class without "id".
class idRenderWorld;
RenderWorld.h // public virtual idRenderWorld interface
RenderWorld_local.h // definition of class idRenderWorldLocal
RenderWorld.cpp // implementation of idRenderWorldLocal
翻译如下:
常规约定
-------
使用常规的tabs相当于4个空格。
通常,不管在哪里,都使用尾随的大括号(这些如:if、else、函数、结构体、定义类型、类定义等等)
if ( x ) {
}
else语句在同一行上的最后一个大括号开始
if ( x ) {
} else {
}
在括号内,空一个空格后再使用表达式或者变量
if ( x ) {
}
而不是
if (x) {
}
还有就是
x = ( y * 0.5f );
而不是
x = (y * 0.5f);
除非有一个明确的需要一个双精度数值,否则使用浮点值的精度规范
float f =0.5f;
而不是
float f =0.5;
和
float f =1.0f;
而不是
float f =1.f;
函数名以大写开始:
void Function( void );
在多词函数名中,每个单词以大写开始:
void ThisFunctionDoesSomething( void);
每个函数开头写的注释的标准是:
/*
====================
函数名
说明
====================
*/
变量名以小写字母开头。
float x;
在多字变量名称的第一个字以小写字符开始的,每一个连续的字开始用大写。
float maxDistanceFromPlane;
定义类型名的命名规则和变量名的命名规则是一样的,不过定义类型名是以“_t”结尾的。
typedef int fileHandle_t;
结构体名的命名规则和变量名的命名规则是一样的,不过结构体名是以“_t”结尾的。
struct renderEntity_t;
枚举名的命名规则和变量名的命名规则是一样的,不过枚举名是以“_t”结尾的。枚举常量全部使用大写字符。多个单词的枚举变量名使用下划线将其分隔。
enum contact_t {
CONTACT_NONE,
CONTACT_EDGE,
CONTACT_MODELVERTEX,
CONTACT_TRMVERTEX
};
递归函数的名称以“_r”结束
void WalkBSP_r( int node );
宏定义名全部使用大写字符。多个单词的宏定义名使用下划线将其分隔。
#defineSIDE_FRONT 0
尽可能地使用“const”
使用
const int*p; // pointer to const int
int *const p; // const pointer to int
const int * const p; // const pointer to const int
而不使用
int const*p;
类
-------
每个类的开头的标准是:
/*
===============================================================================
描述
===============================================================================
*/
类名以“id”开始,后面的词以一个大写字符开始。
classidVec3;
类变量名的命名规则和变量名的命名规则是一样的。
class idVec3 {
float x;
float y;
float z;
}
类方法的命名规则和函数的命名规则一样。
class idVec3 {
float Length( void ) const;
}
为了制作整齐的列,我们会缩进类变量和类方法的变量。变量类型或方法的返回类型是在第一列中,变量名或方法名称在第二列。
class idVec3 {
float x;
float y;
float z;
float Length( void ) const;
constfloat * ToFloatPtr( void ) const;
}
*的指针在第一列,因为它当被认为是部分的类型从而提高了可读性。
类的变量和方法的顺序应该和下面的一样:
1.列出友元类
2.公有变量
3.公有方法
4.保护变量
5.保护方法
6.私有变量
7.私有方法
这使得公共接口在类的开始很容易发现。
当类方法不修改任何类