目录
内容:
缩进
使用空格,而不是制表符。 选项卡应该只出现在需要它们来获得语义含义的文件中,例如 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();