OpenFOAM/C++代码风格规范
-
普适性准则
-
每行最大80个字符
-
缩进默认4个占位符
-
使用空格进行缩进,而不是TAB按键
-
if,else,while,case,for使用的时候附加空格,forAll后不附加空格 -
<<前默认空置4个字符并对齐如
Info<< ... os << ...下面这种是不正确的:
WarningInFunction << "Warning message" -
省略没必要的注释
-
类声明注释居中显示如
/*---------------------------------------------------------------------------*\ Class exampleClass Declaration \*---------------------------------------------------------------------------*/
头文件
-
头文件每个代码块之间空2行
-
使用
//-开始注释 -
内联函数单独放置在
...I.H文件中
C文件
-
C文件中对每个函数的名称空间单独包含
如
Foam::shit::what()是正确的,其中Foam表示名称空间,shit表示类,what表示函数下面是不正确的:
namespace Foam { shit::what() }如果存在多级名称空间,可在C文件中多级包含
潜规则
-
小数据如
scalar,label等通过拷贝传入,其他大数据通过引用传递参数 -
const,尽可能的用他! -
变量初始化使用
=如const className& variableName = otherClass.data();而不是
const className& variableName(otherClass.data());
多行代码
-
多行代码要左对齐
-
const不能单独成行,要和返回类型或参数在一行,如const Foam::longReturnTypeName& //const和返回类型一行 Foam::longClassName::longFunctionName const下面是错误的
const Foam::longReturnTypeName& Foam::longClassName::longFunctionName const //没有左对齐const Foam::longReturnTypeName& Foam::longClassName::longFunctionName const //const单独成行const Foam::longReturnTypeName& Foam::longClassName:: longFunctionName const //::没放在一起如果名字实在场,可以这么搞
const Foam::longReturnTypeName& Foam::veryveryveryverylongClassName:: veryveryveryverylongFunctionName const -
=可作为换行的标,且换行后要缩进 -
如果
=后面的函数需要换行,则在=后换行,如variableName = longClassName.longFunctionName ( longArgument1, longArgument2 );这个是不对的:
variableName = longClassName.longFunctionName ( longArgument1, longArgument2 );
数学符号
-
空格范例:
a + b, a - b a*b, a/b a & b, a ^ b a = b, a != b a < b, a > b, a >= b, a <= b a || b, a && b -
=等号换行,换行的时候,第一个字母或者括号开始于第4个字符处variableName = a*(a + b) *exp(c/d) *(k + t);variableName = ( a*(a + b) *exp(c/d) *(k + t) );
-