Navigation

    CFD中文网

    CFD中文网

    • Login
    • Search
    • 最新

    OpenFOAM/C++代码风格规范

    OpenFOAM
    3
    4
    6000
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 李东岳
      李东岳 管理员 last edited by 李东岳

      普适性准则

      • 每行最大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)
        );
        

      参考:https://openfoam.org/dev/coding-style-guide/

      CFD高性能服务器 http://dyfluid.com/servers.html

      S 1 Reply Last reply Reply Quote
      • S
        sysuzyj @李东岳 last edited by

        @东岳 刚好用来扫盲,谢谢

        1 Reply Last reply Reply Quote
        • 李东岳
          李东岳 管理员 last edited by

          4年前竟然翻译过

          CFD高性能服务器 http://dyfluid.com/servers.html

          Y 1 Reply Last reply Reply Quote
          • Y
            YuPeng @李东岳 last edited by

            @李东岳 感谢感谢,写得好!

            1 Reply Last reply Reply Quote
            • First post
              Last post

            CFD中文网 | 东岳流体 | 京ICP备15017992号-2
            论坛登录问题反馈可联系 li.dy@dyfluid.com