zoukankan      html  css  js  c++  java
  • Google C++ style guide——格式

    1.行长度
    每一行代码字符数不超过80。
    例外:
    1)假设一行凝视包括了超过80字符的命令或URL,出于复制粘贴的方便能够超过80字符;
    2)包括长路径的能够超出80列,尽量避免;
    3)头文件保护能够无视该原则


    2.非ASCII字符
    尽量不使用非ASCII字符,使用时必须使用UTF-8格式。
    尽量不将字符串常量耦合到代码中。比方独立出资源文件。




    3.空格还是制表位
    仅仅使用空格,每次缩进2个空格。
    使用空格进行缩进,不要在代码中使用tab,设定编辑器将tab转为空格。


    4.函数声明与定义
    返回类型和函数名在同一行,合适的话,參数也放在同一行。
    注意一下几点:
    1)返回值总是和函数名在同一行。
    2)左圆括号总是和函数名在同一行;
    3)函数名和左圆括号间没有空格;
    4)圆括号和參数间没有空格;
    5)左大括号总在最后一个參数同一行的末尾处。
    6)右大括号总是单独位于函数最后一行。
    7)右圆括号和左大括号间总是有一个空格。
    8)函数声明和实现处的全部形參名称必须保持一致;
    9)全部形參应尽可能对齐。
    10)缺省缩进为2个空格;
    11)独立封装的參数保持4个空格的缩进。


    假设函数为const的,keywordconst应与最后一个參数位于同一行。


    假设有些參数没实用到,在函数定义出将參数名凝视起来。




    5.函数调用
    尽量放在同一行,否则,将实參封装在圆括号里:
    bool retval = DoSomething(argumengt1,argument2,argument3);
    假设同一行放不下。可断为多行,后面每一行都和第一个实參对齐。左圆括号和右圆括号前不要留空格:
    bool retval = DoSomething(averyveryverylongargument1,argument2,argument3);
    假设函数參数比較多,能够处于可读性的考虑每行仅仅放一个參数:
    bool retval = DoSomething(argument1,
                              argument2,
                              argument3,
                              argument4);
    假设函数名太长。以至于超过行最大长度。能够将全部參数独立成行:
    if(...) {
      ...
      ...
      if(...) {
        DoSomethingThatRequiresALongFunctionName(
          very_long_argument1,
          argument2,
          argument3,
          argument4);
      }
    }


    6.条件语句
    更提倡不在圆括号里加入空格,keywordelse另起一行。
    对基本条件语句有两种能够接受的格式。一种在圆括号和条件之间有空格,一种没有。
    选择哪一种。还是以一致性为主。
    注意全部情况下,if和左圆括号间有个空格。右圆括号和左大括号间也要有个空格。


    if(condition)       // Bad - space missing after IF.
    if (condition){      // Bad - space missing before {.
    if(condition){       // Doubly bad.
    if (condition) {     // Good - proper space after IF and before {
    通常,单行语句不须要使用大括号,假设你喜欢也无可厚非,也有人要求if必须使用大括号。
    但假设语句中哪一分支使用了大括号的话,其它部分也必须使用:
    // Not allowed - curly on IF but not ELSE
    if (condition) {
      foo;
    } else
      bar;
    // Not allowed - curly on ELSE but not IF
    if (condition)
      foo;
    else { 
      bar;
    }
    // Curly braces around both IF and ELSE required because
    // one of the clauses used braces.
    if (condition) {
      foo;
    } else {
      bar;
    }


    7.循环和开关选择语句
    switch语句能够使用大括号分块。空循环体应使用{}或continue。


    switch 语句中的 case 块能够使用大括号也能够不用,取决于你的喜好,使用时要依下文所述。
    假设有不满足 case 枚举条件的值。要总是包括一个 default(假设有输入值没有 case 去处理,编译器将
    报警)。

    假设 default 永不会运行,能够简单的使用 assert:
    switch (var) {
      case 0: {     // 2 space indent
        ...         // 4 space indent
        break;
      }
      case 1: {
        ...
        break;
      }
      default: {
        assert(false);
      }
    }
    空循环体应使用{}戒 continue,而丌是一个简单的分号:
    while (condition) {
      // Repeat test until it returns false.
    }
    for (int i = 0; i < kSomeNumber; ++i) {}   // Good - empty body.
    while (condition) continue;      // Good - continue indicates no logic.
    while (condition);         // Bad - looks like part of do/while loop.


    8.指针和引用表达式
    句点(.)或箭头(->)前后不要有空格,指针/地址操作符(*,&)后不要有空格。
    在声明指针变量或參数时,星号和类型或变量名紧挨都能够:
    // These are fine, space preceding.
    char *c;
    const string &str;
    // These are fine, space following.
    char* c;        // but remember to do "char* c, *d, *e, ...;"!
    const string& str;
    char * c;       // Bad - spaces on both sides of *
    const string & str;    // Bad - spaces on both sides of &
    同一个文件(新建或现有)中起码要保持一致。




    9.布尔表达式
    假设一个布尔表达式超过标准行宽(80字符)。假设要断行要统一一下。
    if (this_one_thing > this_other_thing &&
      a_third_thing == a_fourth_thing &&
      yet_another & last_one) {
      ...
    }


    10.函数返回值
    return表达式中不要使用圆括号。
    函数返回时不要使用圆括号。


    11.变量及数组初始化。
    选择=还是()。


    12.预处理指令
    预处理指令不要缩进。从行首開始。
    即使预处理指令位于缩进代码块中。指令也应从行首開始。
    // Good - directives at beginning of line
    if (lopsided_score) {
    #if DISASTER_PENDING        // Correct -- Starts at beginning of line
      DropEverything();
    #endif
      BackToNormal();
    }
    // Bad - indented directives
    if (lopsided_score) {
      #if DISASTER_PENDING   // Wrong!   The "#if" should be at beginning of line
      DropEverything();
      #endif                   // Wrong!   Do not indent "#endif"
      BackToNormal();
    }


    13.类格式
    声明属性依次序是public、protected、private。每次缩进一个空格。
    除第一个关键词外。其它关键词前空一行,假设类比較小的话也能够不空。


    14.初始化列表
    构造函数初始化列表放在同一行或按四格缩进并排几行。


    两种能够接受的初始化列表格式:
    // When it all fits on one line:
    MyClass::MyClass(int var) : some_var_(var), some_other_var_(var + 1) {

    // When it requires multiple lines, indent 4 spaces, putting the colon on
    // the first initializer line:
    MyClass::MyClass(int var)
        : some_var_(var),              // 4 space indent
        some_other_var_(var + 1) {     // lined up
      ...
      DoSomething();
      ...
    }


    15.命名空间格式化
    命名空间内容不缩进。
    命名空间不加入额外缩进层次,比如:
    namespace {


    void foo() {    // Correct.   No extra indentation within namespace.
      ...
    }


    }    // namespace
    不要缩进:
    namespace {


    // Wrong.   Indented when it should not be.
      void foo() {
        ...
      }


    }   // namespace


    16.水平空白
    水平空白的使用因地制宜。

    不要在行尾加入无谓的空白。




    17.垂直空白
    垂直空白越少越好。
    不要在两个函数定义之间空超过2行,函数体头、尾不要有空行。函数体中也不要任意加入空行。

  • 相关阅读:
    洛谷P2414 阿狸的打字机
    洛谷P4362 贪吃的九头龙
    洛谷P4198 楼房重建
    JSP九大对象
    关于ErrorPage
    通过自己制作网页解决几个简单问题的方法
    九项重要的职业规划提示(转自W3School )
    jquery 使用方法(转载)
    <script language = "javascript">, <script type = "text/javascript">和<script language = "application/javascript">(转)
    微软当年的浏览器之战
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6905576.html
Copyright © 2011-2022 走看看