



























如果它使你的代码看起来不好,你可以打破任何一个规则 。
// Wrong int a, b; char *c, *d; // Correct int height; int width; char *nameOfThis; char *nameOfThat;
// Wrong short Cntr; char ITEM_DELIM = '/t'; // Correct short counter; char itemDelimiter = '/t';
在Qt例子编写中,对变量名有如下建议:
如果参数名和成员变量名发生冲突,使用 "this->" 解决
void MyClass::setColor(const QColor &color;)
{
this->color = color;
}
或
void MyClass::setColor(const QColor &newColor;)
{
color = newColor;
}
避免使用 (意义不明确的字符):
void MyClass::setColor(const QColor &c)
{
color = c;
}
注意:在构造函数中,会遇到同样的问题。但无论你信与不信,下面的可以工作
MyClass::MyClass(const QColor &color;)
: color(color)
{
}
// Wrong
if(foo){
}
// Correct
if (foo) {
}
对指针和引用,在类型和*、&之间加一个空格,但在*、&与变量之间不加空格
char *x; const QString &myString; const char * const y = "hello";
// Wrong char* blockOfMemory = (char* ) malloc(data.size()); // Correct char *blockOfMemory = reinterpret_cast<char *>(malloc(data.size()));
//Wrong x = rect.x(); y = rect.y(); width = rect.width(); height = rect.height();
// Wrong
if (codec)
{
}
// Correct
if (codec) {
}
class Moo
{
};
// Wrong
if (address.isEmpty()) {
return false;
}
// Correct
if (address.isEmpty())
return false;
if (x) {
// do something strange
yyyyyyyyy = yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy +
zzzzzzzzzzzzzzzzzzzzzz;
}
// Correct
if (address.isEmpty() || !isValid()
|| !codec) {
return false;
}
// Wrong
if (address.isEmpty())
return false;
else {
qDebug("%s", qPrintable(address));
++it;
}
// Correct
if (address.isEmpty()) {
return false;
} else {
qDebug("%s", qPrintable(address));
++it;
}
// Wrong
if (a)
if (b)
...
else
...
// Correct
if (a) {
if (b)
...
else
...
}
// Wrong
while (a);
// Correct
while (a) {}
// Wrong if (a && b || c) // Correct if ((a && b) || c) // Wrong a + b & c // Correct (a + b) & c
switch (myEnum) {
case Value1:
doSomething();
break;
case Value2:
doSomethingElse();
// fall through
default:
defaultHandling();
break;
}
// Correct
if (longExpression
+ otherLongExpression
+ otherOtherLongExpression) {
}
例外:如果使用的 if 语句 和 && 或者 ||,对齐需要一点调整(否则控制语句和body会较难以分辨)
//Wrong
if (dsfljfsfskjldsjkljklsjdk
&& fdsljsjdsdljklsjsjkdfs
&& dsfljkdfjkldksdfjdjkfdksfdkjld) {
sadjdjddadhsad;
}
//Correct
if (dsfljfsfskjldsjkljklsjdk
&& fdsljsjdsdljklsjsjkdfs
&& dsfljkdfjkldksdfjdjkfdksfdkjld) {
sadjdjddadhsad;
}
对 whle 或else if,不存在这个问题:
while (dsfljfsfskjldsjkljklsjdk
&& fdsljsjdsdljklsjsjkdfs
&& dsfljkdfjkldksdfjdjkfdksfdkjld) {
sadjdjddadhsad;
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。