Source Code Style

class

Use the following order for access specifiers. Put all constructors first followed by any destructors.

class QWidget : public QObject
{
   CS_OBJECT(QWidget)

   enum Categories {
      ItemA,
      ItemB,
   }

   public:
      // constructors
      // destructors 

      size_type chop( );

   protected:
      // 

   private:
      someDataType m_data;

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

Brace wrapping, AfterClass: true

 

struct or enum

For a struct or enum leave the curly up.

struct {
   int m_count;
};

Brace wrapping, AfterStruct: false

 

namespace

Do not indent the code inside a namespace.

namespace SomeName {

int doThis()
{
   . . .
}

} // namespace someName

 

constructors and destructors

Ginger(int data)
   : Parent()
{
}

SplitEmptyFunction: true

 

inline methods

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

AfterFunction: false

 

out-of-line methods

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

 

templates

Declarations and implementations should have the template parameters on the first line and the function or method name on the next line.

template<typename T>
bool convert()

 

constructs

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

 

switch

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

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

IndentCaseLabels: true

 

unused parameter

If a parameter is not used then feel free to remove the name in the argument list in the cpp file. Do not remove the name in the class declaration located in a public header 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 code to remove a possible warning.

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. Two is too narrow and four stretches indented code out too far.

Do not use tabs.

 

semicolon

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

Do not use a semicolon concluding a namespace.

 

variable names

Variable names should have some semantic meaning and not be excessivly long. Avoid using single letters or names like "li" or "it". For class member variables use "m_" before the rest of the variable name.

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

for (auto &item : some_container)

Use iter, iter_begin, or iter_end instead of "it".

It is fine to use i , j , or k inside a for loop if the variable meaning is just as a loop counter.

 

return variable name

For the return variable name use retval. This is a variable name which should not be used for any other meaning to avoid conflicts.

   int retval = 42;

   ...

   return retval;

 

parameter names

For copy and move constructors, assignment operators, and swap, use the parameter name other.

   MyClass(const & other);
   MyClass& operator=(const & other);

 

variable casing

Variable names with CamelCase is encouraged and preferred for readability. One exception is for conformance with the STL for things like size_type and const_iterator.

 

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.

 

Use blank lines for readability

Place a blank line at the start of a long or difficult to read block when it makes the code easier to read.

Configured with KeepEmptyLinesAtTheStartOfBlocks: true.

 

align bit fields

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

AlignConsecutiveBitFields: true

 

unit tests

▸ Include Files

Include CopperSpice headers and then the header <cs_catch2.h>.

▸ Macros

Use REQUIRE(x) instead of CHECK(x)

▸ Variable Names

Prefer the name data or data1, data2, data3 for the variable name being tested.

▸ Code

1 Place any support code such as classes or structs before all unit tests.

2 Use auto only for iterator data types, never for any other data types.