diff --git a/编码规范.md b/编码规范.md index 86f2812..08c87b1 100644 --- a/编码规范.md +++ b/编码规范.md @@ -17,8 +17,8 @@ ### 缩进 -- 采用4个空格 -- 空格,不要用TAB! +- 采用4个空格进行缩进 +- 使用空格,不要用TAB! ### 变量声明 @@ -36,10 +36,10 @@ int height; int width; char *nameOfThis; - char '''nameOfThat; + char *nameOfThat; ``` -- 以小写字符开头,后续单词以大写开头 +- 首字母小写,后续单词以大写开头(驼峰式) - 避免使用缩写 ```cpp @@ -52,7 +52,7 @@ char itemDelimiter = ''; ``` -- 类名总是以大写开头。公有类以Q开头(QRgb),公有函数通常以q开头(qRgb)。 +- 类名总是以大写开头。 ### 空白 @@ -62,11 +62,13 @@ ```cpp // 错误示例 - if(foo){ + if(foo) + { } // 正确 - if (foo) { + if (foo) + { } ``` @@ -75,7 +77,7 @@ ```cpp char *x; const QString &myString; - const char''' const y = "hello"; + const char* const y = "hello"; ``` - 二元操作符前后加空白 @@ -87,45 +89,49 @@ char* blockOfMemory = (char* ) malloc(data.size()); // 正确 - char '''blockOfMemory = reinterpret_cast(malloc(data.size())); + char *blockOfMemory = reinterpret_cast(malloc(data.size())); ``` ### 大括号 -- 基本原则:左大括号和语句保持在同一行: +- 基本原则:左大括号和语句之间换行,左大括号总是单独占一行: ```cpp // 错误示例 - if (codec) - { + if (codec){ } - // 正确 - if (codec) { + // 正确 + if (codec) + { } ``` -- 例外:函数定义和类定义中,左大括号总是单独占一行: +- 例外1:定义命名空间时,左大括号与命名空间同行: ```cpp - static void foo(int g) - { + namespace Test { qDebug("foo: %i", g); } - class Moo - { - }; ``` +- 例外2:控制语句的body为空时,大括号与控制语句同行: + +```cpp + while(a) {} +``` + - 控制语句的body中只有一行时不使用大括号 ```cpp // 错误示例 - if (address.isEmpty()) { + if (address.isEmpty()) + { return false; } - for (int i = 0; i < 10; +''i) { + for (int i = 0; i < 10; +''i) + { qDebug("%i", i); } @@ -141,45 +147,51 @@ ```cpp // 正确 - if (address.isEmpty() || !isValid() - || !codec) { - return false; - } + if (address.isEmpty() || !isValid() + || !codec) + { + return false; + } ``` - 例外2:在if-else结构中,有一处跨多行,则使用大括号 ```cpp // 错误示例 - if (address.isEmpty()) - return false; - else { - qDebug("%s", qPrintable(address)); -it; - } + if (address.isEmpty()) + return false; + else + { + qDebug("%s", qPrintable(address)); + it; + } // 正确 - if (address.isEmpty()) { - return false; - } else { - qDebug("%s", qPrintable(address)); -it; - } + if (address.isEmpty()) + { + return false; + } + else + { + qDebug("%s", qPrintable(address)); + it; + } // 错误示例 - if (a) - if (b) - … - else - … + if (a) + if (b) + … + else + … // 正确 - if (a) { - if (b) - … - else - … - } + if (a) + { + if (b) + … + else + … + } ``` - 如果控制语句的body为空,则使用大括号 @@ -216,7 +228,8 @@ it; - 每一个case必须有一个break(或renturn)语句,或者用注释说明无需break ```cpp - switch (myEnum) { + switch (myEnum) + { case Value1: doSomething(); break; @@ -238,13 +251,17 @@ it; // 正确 if (longExpression + otherLongExpression - + otherOtherLongExpression) { + + otherOtherLongExpression) + { + ... } // Wrong if (longExpression + otherLongExpression + - otherOtherLongExpression) { + otherOtherLongExpression) + { + ... } ``` @@ -261,4 +278,4 @@ it; 1. [Qt编码风格/Qt Coding Style](https://wiki.qt.io/Qt_Coding_Style) 2. [LLVM编码标准/LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html) 3. [谷歌C++风格指南/Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) - 4. [C++核心指南/Cpp Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) \ No newline at end of file + 4. [C++核心指南/Cpp Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)