Skip to content

Coding style

Francesco Biscani edited this page Apr 17, 2020 · 16 revisions

Developing for PaGMO requires minimum a C++17 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.

clang-format is installed as part of the LLVM toolchain, and it is available on most operating systems from a variety of package managers. clang-format plugins are available for most popular text editors (vi, emacs, atom, vscode, etc.). Note that pagmo's .clang-format config file resides in the root of the source tree.

Alternatively, you can manually run clang-format on the pagmo source files that you are editing:

$ clang-format -i filename.hpp

The command should be run from the root of pagmo's source tree.

Code formatting (Python)

The python code in pygmo follows the PEP8 standard. Many popular editors support automatic linting of Python source files according to PEP8, either natively or with external plugins. You can also use the autopep8 utility manually from the command line, if you prefer.

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;
};