Skip to content

Coding style

Dario Izzo edited this page Mar 22, 2017 · 16 revisions

Developing for PaGMO requires minimum a C++11 compiler. The code basis was coded around the idea of exploiting the new feature offered by this standard. So, when you code a part of PaGMO, consider all the new possibilities that this standard introduced.

Code formatting

All code in PaGMO is formatting using clang-format

Class style

In general, whenever possible, follow the rule of 5/3/0: http://en.cppreference.com/w/cpp/language/rule_of_three. As a rule of thumb, if a class does not need special constructors, destructor or assignment operators, none should be implemented. If, however, at least one of them needs to have a non-default implementation, all 5 should be explicitly declared and defined (using the default keyword as appropriate). Constructors other than the default, move and copy constructors should be marked as explicit.

Which and how many headers?

Remember the following fundamental rule:

  • If you introduce in a file my_file.hpp a symbol defined elsewhere (a class, a function a struct, anything with a name) you must also add the header where the symbol is defined to the include list on top of the file. Regardless of whether some other headers is already including it indirectly.
  • If you remove in a file my_file.hpp a symbol defined elsewhere (a class, a function a struct, anything with a name) you must also remove the header where the symbol is defined to the include list on top of the file.
  • First list the system <> headers, then the project "".
  • Headers must be always included in alphabetical order

Tests

Every new class must be paralleled by a new test file in the folder tests. Any new method or feature must be paralleled with a new test in the corresponding file in the folder tests.

Banned habits

The following habits, are banned from PaGMO:

  • Camel Case naming
  • use of tabs
  • using std;

Example Class:

#include <random>
#include <vector>
class A 
{
public:
    // default constructor
    A() = default;
    // constructor from int
    explicit A(int a) : m_data(a) {};
    // copy constructor
    A(const A &other) = default;
    // move constructor
    A(A &&other) = default;
    // copy assignment
    A &operator=(const A &) = default;
    // move assignment
    A &operator=(A &&) = default;
    const int& get_data()
    {
        return m_data;
    }
    void compute(const std::vector<double> &x)
    {
        for (const auto &item : x) {
            item = 0;
        }
    }
};
Clone this wiki locally