This is a overview of the coding convention we use when writing Qt code. The data has been gathered by mining the Qt sources, discussion forums, email threads and through collaboration of the developers.
// Wrong int a, b; char *c, *d; // Correct int height; int width; char *nameOfThis; char *nameOfThat;
name starts with a capital letter
// Wrong short Cntr; char ITEM_DELIM = '\t'; // Correct short counter; char itemDelimiter = '\t';
// 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 if (codec) { } // Correct if (codec) { }
static void foo(int g) { qDebug("foo: %i", g); } class Moo { };
and also if a single line statement is somewhat complex.
// 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);
// 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) { } // Wrong if (longExpression + otherLongExpression + otherOtherLongExpression) { }