美文网首页
C++编码规范

C++编码规范

作者: 丰宁 | 来源:发表于2017-08-08 17:10 被阅读22次

C++编码规范

总原则

代码是给人看的,要让你的代码看上去清晰、美观。

C++ features C++特性

  • 不要使用异常
  • 不要使用rtti(Run-Time Type Information; 类似于dynamic_cast, typeid操作符,抛出异常等)
  • 适当地使用模板技术,不要炫技

Casting 类型强制转换

  • 尽量少使用类型强制转换,如果一定要用,避免使用C风格强制转换,使用C++风格的类型强制转换(static_cast, const_cast, reinterpret_cast)
  • 使用类构造函数进行基础数据类型的类型转换
// Wrong
 (int)myFloat

// Correct
int(myFloat);

Compiler/Platform specific issues 编译器/平台特定问题

Aesthetics 美学

Things to avoid 要避免的做法

  • 不要从模板或者工具类派生
  • 不要混用const和非const迭代器
for (Container::const_iterator it = c.begin(); it != c.end(); ++it) // W R O N G
for (Container::const_iterator it = c.cbegin(); it != c.cend(); ++it) // Right

Namespacing 命名空间

  • 适当地划分命名空间有助于代码的组织和复用

Operators 操作符

C++11

  • auto关键字。auto有的时候能够带来便利,但有的时候会让代码难以阅读。以下两种情况鼓励使用auto:

    • 当auto可以避免在同一条语句中重复出现类型信息时
    auto something = new MyCustomType;
    auto keyEvent = static_cast<QKeyEvent *>(event);
    auto myList = QStringList() << QLatin1String("FooThing") << QLatin1String("BarThing");
    
    • 当使用迭代器时
    auto it = myList.const_iterator();
    

Indentation 缩进

  • 用4个空格缩进
  • 记住,是空格,不是Tab

Declaring variables 声明变量

  • 每个变量声明单独一行
  • 避免使用太短的或者没有意义的名称(如: "a", "rbarr", "nughdeget")
  • 只有在需要计数变量或者临时变量的地方使用单字母变量(如i, j, k),因为这些地方变量的用处很明显,不容易有歧义
  • 在需要使用的时候再声明变量
// Wrong
int a, b;
char *c, *d;

// Correct
int height;
int width;
char *nameOfThis;
char *nameOfThat;
  • 变量和函数的名称以小写字符开头,之后的每个单词首字母大写。
  • 尽量避免使用缩写
// Wrong
short Cntr;
char ITEM_DELIM = ' ';

// Correct
short counter;
char itemDelimiter = ' ';
  • 类名称首字母大写
  • 首字母缩写词采用驼峰方式(camel-case)(如XMLStreamReader应该写成XmlStreamReader)

空白

  • 用空行对代码逻辑块进行分隔
  • 使用一个空行就够了
  • 在C++关键词之后后以及左花括号之前留出一个空格
// Wrong
if(foo){
}

// Correct
if (foo) {
}
  • 对于指针和引用,在类型名和'*'或'&'之间留出一个空格,但在变量名和'*'或'&'之间不要留空格
char *x;
const QString &myString;
const char * const y = "hello";
  • 在二元操作符前后留空格
  • 在类型强制转换后不要留空格
  • 尽量避免C风格的类型强制转换
// Wrong
char* blockOfMemory = (char* ) malloc(data.size());

// Correct
char *blockOfMemory = reinterpret_cast<char *>(malloc(data.size()));
  • 在一行内不要出现多条语句
  • 在控制流语句(如if, else等)后使用一个空行
// Wrong
if (foo) bar();

// Correct
if (foo)
    bar();

Braces 花括号

  • 函数实现或者类声明后的大括号必须新起一行
static void foo(int g)
{
    qDebug("foo: %i", g);
}

class Moo
{
};
  • 在条件语句内容体只有一行时,不要使用大括号
// Wrong
if (address.isEmpty()) {
    return false;
}

for (int i = 0; i < 10; ++i) {
    qDebug("%i", i);
}

// Correct
if (address.isEmpty())
    return false;

for (int i = 0; i < 10; ++i)
    qDebug("%i", i);
  • 例外1:当条件语句判断条件本身太长而出现换行时,内容体都应该使用大括号
// Correct
if (address.isEmpty() || !isValid()
    || !codec) {
    return false;
}
  • 例外2:如果if或者else两个内容体中的任何一个使用了大括号,那么两个内容体都应该使用大括号
// Wrong
if (address.isEmpty())
    qDebug("empty!");
else {
    qDebug("%s", qPrintable(address));
    it;
}

// Correct
if (address.isEmpty()) {
    qDebug("empty!");
} else {
    qDebug("%s", qPrintable(address));
    it;
}

// Wrong
if (a)
    …
else
    if (b)
        …

// Correct
if (a) {
    …
} else {
    if (b)
        …
}

Parentheses 圆括号

  • 使用圆括号对表达式进行分组
// Wrong
if (a && b || c)

// Correct
if ((a && b) || c)

// Wrong
a + b & c

// Correct
(a + b) & c

Switch statements Switch语句

  • case和switch左对齐
  • 每个case必须有一个对应的break,除非你是故意的,并且添加了相应注释
switch (myEnum) {
case Value1:
  doSomething();
  break;
case Value2:
case Value3:
  doSomethingElse();
  // fall through
default:
  defaultHandling();
  break;
}

Jump statements (break, continue, return, and goto) 跳转语句(break, continue, return 以及 goto)

  • 不要在跳转语句后面使用else
// Wrong
if (thisOrThat)
    return;
else
    somethingElse();

// Correct
if (thisOrThat)
    return;
somethingElse();
  • 例外: 如果为了大段代码的对称性,可以不这么做

Line breaks 断行

  • 保持每行代码的字符数小于100
  • 逗号放在断行尾,操作符放在新行开头
// Wrong
if (longExpression +
    otherLongExpression +
    otherOtherLongExpression) {
}

// Correct
if (longExpression
    + otherLongExpression
    + otherOtherLongExpression) {
}
  • 当条件语句的内容体为空时,仍然应该使用大括号
// Wrong
while (a);

// Correct
while (a) {}

General exception 例外

如果以上的任何规则让你的代码看起来很丑,不要傻兮兮的照做。

相关文章

网友评论

      本文标题:C++编码规范

      本文链接:https://www.haomeiwen.com/subject/kwtxrxtx.html