Webkit网络套件 代码风格指南

目录

内容:

缩进

Spacing

Line breaking

Braces

Null, false and zero

Floating point literals

名称

Other Punctuation

Pointers and References

#include Statements

“using” Statements

类型

Classes

Singleton pattern

Comments

Overriding Virtual Methods

Python


缩进

使用空格,而不是制表符。 选项卡应该只出现在需要它们来获得语义含义的文件中,例如 Makefile。

缩进大小为 4 个空格。

对:

int main()
{
    return 0;
}

错误的:

int main() 
{
        return 0;
}

命名空间的内容不应缩进。

对:

// Document.h
namespace WebCore {

class Document {
    Document();
    ...
};

namespace NestedNamespace {

class OtherDocument {
    OtherDocument();
    ...
};

}

} // namespace WebCore

// Document.cpp
namespace WebCore {

Document::Document()
{
    ...
}

namespace NestedNamespace {

OtherDocument::OtherDocument()
{
    ...
}

} // namespace NestedNamespace

} // namespace WebCore

对:

// PrivateClickMeasurementDatabase.h
namespace WebKit::PCM {

class Database {
    ...
};

} // namespace WebKit::PCM

错误的:

// Document.h
namespace WebCore {

    class Document {
        Document();
        ...
    };

    namespace NestedNamespace {
    ...
    }

} // namespace WebCore

// Document.cpp
namespace WebCore {

    Document::Document()
    {
        ...
    }

} // namespace WebCore

case 标签应该与其 switch 语句对齐。 case 语句是缩进的。

对:

switch (condition) {
case fooCondition:
case barCondition:
    i++;
    break;
default:
    i--;
}

错误的:

switch (condition) {
    case fooCondition:
    case barCondition:
        i++;
        break;
    default:
        i--;
}

Boolean expressions at the same nesting level that span multiple lines should have their operators on the left side of the line instead of the right side.

Right:

return attribute.name() == srcAttr
    || attribute.name() == lowsrcAttr
    || (attribute.name() == usemapAttr && attribute.value().string()[0] != '#');

Wrong:

return attribute.name() == srcAttr ||
    attribute.name() == lowsrcAttr ||
    (attribute.name() == usemapAttr && attr->value().string()[0] != '#');

Spacing

Do not place spaces around unary operators.

Right:

i++;

Wrong:

i ++;

Do place spaces around binary and ternary operators.

Right:

y = m * x + b;
f(a, b);
c = a | b;
return condition ? 1 : 0;

Wrong:

y=m*x+b;
f(a,b);
c = a|b;
return condition ? 1:0;

Place spaces around the colon in a range-based for loop.

Right:

Vector<PluginModuleInfo> plugins;
for (auto& plugin : plugins)
    registerPlugin(plugin);

Wrong:

Vector<PluginModuleInfo> plugins;
for (auto& plugin: plugins)
    registerPlugin(plugin);

不要在逗号和分号前放置空格。

对:

for (int i = 0; i < 10; ++i)
    doSomething();

f(a, b);

错误的:

for (int i = 0 ; i < 10 ; ++i)
    doSomething();

f(a , b) ;

在控制语句及其括号之间放置空格。

对:

if (condition)
    doIt();

错误的:

if(condition)
    doIt();

Do not place spaces between a function and its parentheses, or between a parenthesis and its content.

Right:

f(a, b);

Wrong:

f (a, b);
f( a, b );

Do not place spaces between square brackets and parentheses of a lambda function but do place a space before braces.

Right:

[](int x) { return x; }
[this] { return m_member; }

Wrong:

[] (int x) { return x; }
[this]{ return m_member; }

When initializing an object, place a space before the leading brace as well as between the braces and their content.

Right:

Foo foo { bar };

Wrong:

Foo foo{ bar };
Foo foo {bar};

In Objective-C, do not place spaces between the start of a block and its arguments, or the start of a block and its opening brace. Do place a space between argument lists and the opening brace of the block.

Right:

block = ^{
...
};

block = ^(int, int) {
...
};

Wrong:

block = ^ {
...
};

block = ^ (int, int){
...
};

Line breaking

Each statement should get its own line.

Right:

x++;
y++;
if (condition)
    doIt();

Wrong:

x++; y++;
if (condition) doIt();

An else statement should go on the same line as a preceding close brace if one is present, else it should line up with the if statement.

Right:

if (condition) {
    ...
} else {
    ...
}

if (condition)
    doSomething();
else
    doSomethingElse();

if (condition)
    doSomething();
else {
    ...
}

Wrong:

if (condition) {
    ...
}
else {
    ...
}

if (condition) doSomething(); else doSomethingElse();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值