Development guide
Styles
Classes
By placing the public section first, everything that is of interest to a user is gathered in the beginning of the class definition. The protected section may be of interest to designers when considering inheriting from the class. The private section contains details that should have the least general interest.
Functions
A member function that is defined within a class definition automatically becomes inline. Class definitions are less compact and more difficult to read when they include definitions of member functions. It is easier for an inline member function to become an ordinary member function if the definition of the inline function is placed outside of the class definition.
// Long function declarations: DateTime &assign(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microseconds = 0);
Templates
Compound statements
The placement of braces seems to have been the subject of the greatest debate concerning the appearance of both C and C++ code. We recommend a style that, in our opinion, gives the most readable code. Other styles may well provide more compact code.
Flow control statements
At times, everything that is done in a loop may be easily written on one line in the loop statement itself. It may then be tempting to conclude the statement with a semicolon at the end of the line. This may lead to misunderstanding since, when reading the code, it is easy to miss such a semicolon. Also, the semicolon could be deleted by accident, leading to a hard-to-find bug. It seems to be better, in such cases, to place an empty block after the statement to make completely clear what the code is doing. Even more better is to avoid this style altogether. In certain cases, a code that is more compact might be better readable than code with many blocks containing only a single statement. As a general rule, readability must always be preferred to strict style adherence.
// Blocks and single statements: int ASCIIEncoding::convert(int ch, unsigned char *bytes, int length) const { if (ch >= 0 && ch <= 127) { *bytes = (unsigned char) ch; return 1; } else return 0; }
// Single-line flow control statements can improve readability by keeping the code more compact: void MessageHeader::splitParameters(const std::string &s, std::string& value, NameValueCollection& parameters) { value.clear(); parameters.clear(); std::string::const_iterator it = s.begin(); std::string::const_iterator end = s.end(); while (it != end && isspace(*it)) ++it; while (it != end && *it != ';') value += *it++; trimRightInPlace(value); if (it != end) ++it; splitParameters(it, end, parameters); }