Inspired from http://qt-project.org/wiki/Qt_Coding_Style
This is an overview of the coding conventions to be used when writing code for the QElectroTech project.
void Class::methodName(const QString &string, const QString &suffix = QString()) { int dummy_return_value = -1; if ( string.length() < 567 && !suffix.isEmpty() // forget the dummy code, notice the indentation ) { dummy_return_value = string.lastIndexOf( QRegExp("chabadabada", Qt::CaseInsensitive), 42 ); } return(dummy_return_value); }
object -> method(arg1, arg2); // spaces are used to align arg2 and argument2 object -> method(argument1, argument2);
However, beware of this habit since it always ends up with you having to realign many semantically-untouched lines. The main conseqence of this kind of operations is that basic svn diff, such as those sent by mail after each commit, get less readable. In case you fell in the trap, the -b
or --ignore-space-change
option exposed by both diff and svn diff should come useful.
Regarding indentation, some may argue that horizontal tab is considered a control character and thus not a printable one. Feel free to discuss that topic on a rainy sunday during a power outage, but the QElectroTech project accepts that non-printable character in its code. However, we *flee* non-ASCII characters: insert Unicode characters using HTML entities such as &#xHH; or octal notation such as \0321, but strive on remaining within the warm and well-known ASCII table. That should spare you some compilation/portability issues.
Unix-style only, Unix-style everywhere: lines should be separated with a LF (Line Feed, the Unix way) character, not a CR (Carriage Return, the Mac way) nor a CRLF (the Windows way). The last line of a file should also end with a LF.
// Wrong short Cntr; char ITEM_DELIM = '\t'; int ConductorsCount; // Correct short counter; char item_delimiter = '\t'; int conductors_count;
// Wrong if ( foo ) { } // Wrong if(foo){ } // Correct if (foo) { }
char *x; const QString &my_string; const char * const y = "hello";
my_object -> interestingMethod();
// Wrong char* blockOfMemory = (char* ) malloc(data.size()); // Correct char *block_of_memory = reinterpret_cast<char *>(malloc(data.size()));
// Wrong if (codec) { } // Correct if (codec) { }
VeryInterestingClass::VeryInterestingClass() : simple_attribute(42), complicated_attribute(43) { // more complex code }
// Correct: simple conditions that fit on a single line are ok if (things -> areNotLikeExpected()) return; // Correct: otherwise, use braces if (things -> areNotLikeExpected()) { // you can even use them when there is only one statement... future // modifications will result in commit diff which are easier to review doSomeStuff(foo, bar, yadda_yadda); doSomethingElse(bar, foo, yadda_yadda); } else { // another tip: "} else {" statements are allowed, but splitting the else // part this way make it easier to comment and/or remove beginUsualStuff(foo, bar, yadda_yadda); beginUsualStuff(foo, bar, yadda_yadda); beginUsualStuff(foo, bar, yadda_yadda); } // Wrong: two lines, no braces if (my && shiny && condition || evaluates ^ to_true) return -1; // In case of doubt: use braces.
// Wrong if (a && b || c) // Correct if ((a && b) || c) // Wrong a + b & c // Correct (a + b) & c
/** Represents the kind of a particular conductor: * Simple: no symbols, no text input * Single: singleline symbols, no text input * Multi: text input, no symbol */ enum ConductorType { Simple, Single, Multi };
Describe conventions to follow when declaring a class.