《Google C++ Style Guide》备忘

仔细看了遍Google这个编程规范,写得还真是细致,连哪个地方该不该用空格都有详细规定。而其中一些正是我以前纠结之处。就在此备忘一些以前忽略的一些小细节吧。

1. 将c文件对应的h文件放在include的第一位可减少隐藏依赖 [1.6]

2. 尽量使用++i代替i++,效率更高。[5.10]

3. 尽量使用const,并且写成const int* foo形式较好。[5.11]

4. 尽量不使用无符号数进行循环计数。[5.12]

5. 尽可能用 sizeof(varname) 代替 sizeof(type)。这样保证更改类型后会自动更新。[5.16]

6. 行后注释先空两格。[7.6>

7. 函数名过长,形参如需换行,需缩进两个TAB(8个空格)。 [8.4]

ReturnType LongClassName::ReallyReallyReallyLongFunctionName(

        Type par_name1,  // 8 space indent

        Type par_name2,

        Type par_name3) {

    DoSomething();  // 4 space indent

    …

}

8. 函数名过长,实参如需换行,需缩进1个TAB(4个空格)。 [8.5]

if (…) {

    …

    …

if (…) {

    DoSomethingThatRequiresALongFunctionName(

        very_long_argument1,  // 4 space indent

        argument2,

        argument3,

        argument4);

}

9. 所有情况下 if 和左圆括号间都有个空格. 右圆括号和左大括号之间也要有个空格。[8.6]

if (condition) {  // Good – proper space after IF and before {.

10. 简短的条件语句允许写在同一行. 只有当语句简单并且没有使用 else 子句时使用。[8.6]

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>