Skip to content

Coding style

Dario Izzo edited this page Apr 29, 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 (C++)

All code in PaGMO must be formatted using clang-format. So you are not allowed to decide on stuff like spaces or tabs, where do I like my curly braces etc. Everything is decided for you by clang-format

While developing use this line to format your code before committing. (use it from the pagmo2 dir)

git ls-files | grep -E "\.(in|h|hpp|cpp)$" | grep -vE "external" | xargs clang-format -i find ./ -iname '*.py'|xargs autopep8 -i

Alternatively, clang-format is supported as a plugin by many modern editors (e.g., Sublime, Atom, VS Code, etc.).

Code formatting (Python)

The python code in pygmo follows the PEP8 standard

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.
  • Headers must be always included in alphabetical order (clang-format will take care of this for you).

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
  • 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 (auto &item : x) {
            item = 0;
        }
    }
private:
    int m_data = 0;
};
Clone this wiki locally