All code is ASCII only. UTF-8 if you can't help it (but nothing else). Avoid C casts, prefer C++ casts (static_cast, const_cast, reinterpret_cast). Use the constructor to cast simple types: int(myFloat) instead of (int)myFloat 4 spaces are used for indentation As a base rule, the left curly brace goes on the same line as the start of the statement: Code (cpp): // Wrong if (codec) { } // Correct if (codec) { } Exception: Function implementations and class declarations always have the left brace on the start of a line: Code (cpp): static void foo(int g) { qDebug("foo: %i", g); } class Moo { }; (this one I changed) ALWAYS use curly braces when the statement with a block takes more than 1 line. Code (cpp): // Wrong // While it may be correct for a compiler it is harder to read. 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); } // Unless statement(s) is(are) very long you should consider using single-line form if (address.isEmpty()) return false; for (int i = 0; i < 10; ++i) qDebug("%i", i); Use parentheses to group expressions. Compiler has no problems identifying what should it do (even if it is not what you intended), for humans however it may not be so easy. Code (cpp): // Wrong if (a && b || c) // Correct if ((a && b) || c) // Wrong a + b & c // Correct (a + b) & c Every case must have a break (or return) statement at the end or a comment to indicate that there’s intentionally no break. Commas go at the end of a broken line; operators start at the beginning of the new line. The operator is at the end of the line to avoid having to scroll if your editor is too narrow. Code (cpp): // Correct if (longExpression + otherLongExpression + otherOtherLongExpression) { } // Wrong if (longExpression + otherLongExpression + otherOtherLongExpression) { } Also "Compiler/Platform specific issues" in http://qt.gitorious.org/qt/pages/CodingConventions has these things to say:
I prefer Code (text): if (xx) return abc; to Code (text): if (xx) return abc; a lot. Can I still use it?
Can you insert { } and use something like Code (cpp): if (xx) { return abc; } ? It will also help later if you need to insert more code inside this if without creating {} manually. There will be no need to someone else to guess whether you mean it or made a mistake and forgot {} either.
Nobody counts our lines so it's not a problem (^ ^) You see, someone will add code later and if the said someone will make a slight mistake like this… Code (cpp): if (xx) somethingcool(a, b, c, d, e, f); iamanotherfunctionbutimwild(z, x, c, v, b); The third one will be left to wonder whether 2 lines were supposed to be in the if() block or not. I don't think we actually need that. As weird as it sounds it happens (~_~)
Well, you do. But who knows what others can do… you never know (O.o) Well, that's my line of reasoning anyway (^ ^)
I have my editor set up for a different number of spaces per tab than all of you. Until all the tabs are replaced by spaces, indentation can get very confusing for people like me :) Anyway: Do structs, enums and namespaces get the left brace on the start of a line, like classes and functions?
It depends on the size inside honestly... For functions it's on the start of a line except if it's in the class declaration.
Well, in my opinion, There aren't a lot of ways to do this. :P I usually do Code (cpp): if() { //Everything else } But is there any difference of doing: Code (cpp): if() { //Everything else } ?
Since structs are pretty much classes they are the same. As for enums and namespaces I think they should follow them too (left brace on the start of a line). @Latios: there is no difference. It's just need to be consistent, so we all should use the same way. You don't need a lot of them to wreak havoc :}