Source Code Style

class

class QWidget : public QObject
{
   CS_OBJECT(QWidget)   

   // cs enums

   public:
      size_type chop( );

   protected:
      // 

   private:
      Thing m_data;

      friend bool operator==(const QWidget &a, const QWidget &b);
};

Brace wrapping, AfterClass: true

 

struct

struct {
   int m_count;      
};

Brace wrapping, AfterStruct: false

 

namespace

Do not use indentation.

namespace SomeName {

int doThis() { 
   ...     
}

} // namespace someName

 

constructors

Ginger(int data)
   : Parent() 
{
}

SplitEmptyFunction: true

 

methods

data_type QWidget::count() const {
   return m_data.size();
}

AfterFunction: false

 

constructs

For every if, else, for use curlies even when there is only one line of code in that branch. Prefer the "true" as the first branch.

 

switch

switch (writingSystem) {   
   case Any:
      name = "Any";
      break;

   case Latin:
      name = "Latin";
      break;
}

IndentCaseLabels: true

 

return

For the return variable name use retval. This is a variable name which is not used anywhere else and will not conflict.

   int retval = 42;

   ...

   return retval;

 

unused parameter

If a parameter is not used then remove the name in the argument list in the cpp file. Do not remove the name in the class declaration since it is used for documentation.

bool QFontDatabase::isFixedPitch(const QString &family, const QString &) const
{
}

If the variable is used in some branch of an #ifdef use the following:

bool QFontDatabase::isFixedPitch(const QString &family, const QString & style) const 
{
   (void) style;
}

SpaceAfterCStyleCast: true

 

pointer alignment

const DataType &other;

QList<int *> foobar;

 

spacing

Three spaces. We needed to pick something and this is a nice compromise.

Do not use tabs.

 

semicolon

Required at the end of a class, enum, struct.

Do not use a semicolon concluding a namespace.

 

variable names

Variable names should have some semantic meaning. Do not use single letters or names like "li" or "it". For class member variables use "m_" before the rest of the name.

For example, use "item" for range based for of a container.

for (auto &item : some_container)

 

Digraphs and Trigraphs

Digraphs and trigraphs are sequences of two and three characters in source code which are meant to be treated as if they were single characters.
Use the operators and not the text alternative.

Since Trigraphs were removed in C++17 they simply can not be used.

Operator (correct) Alternative (avoid)
&& and
&= and_eq
& bitand
| bitor
~ compl
! not
!= not_eq
|| or
|= or_eq
^ xor
= xor_eq
{ <%
} %>
[ <:
] :>
# %:
## %:%:

 

range based for

Prefer a range based for when possible.

 

extra lines

Configured with KeepEmptyLinesAtTheStartOfBlocks: true. Use extra lines for readability.

 

align bit fields

This is a new setting in clang-format, not available in the released version. Requires clang 11.

AlignConsecutiveBitFields: true