This document provides a list of rules that need to be followed when writing code that become part of BEAST. As the development of a consistent coding style is work in progress, these rules are not comprehensive. We also acknowledge that not all code that is part of BEAST currently follows this coding style guide. However, everything that is in this file has been negotiated, and new code must follow each of these rules. Where no explicit rule exists, try to follow what existing code does, or ask, when in doubt. Table of Contents Table of Contents 1 · Indentation and Formatting 1.1 · Function Return Types are written on a Separate Line 1.2 · Multiline Comments 1.3 · References and Pointers are declared next to the Identifier 1.4 · Constructor Initializer Indentation 1.5 · Data members in classes are declared before functions 2 · Names 2.1 · Functions, Methods, Members, Arguments and Local Variables are written as lower_case_underscore_names 2.2 · Type Names are written as MixedCaseNames 2.3 · Class Members are prefixed with m_ 3 · C++ References 3.1 · Out Arguments of Functions that are Non Object Types are passed by Pointer, not by Reference 1.1 Function Return Types are written on a Separate Line
This rule only applies for the implementation of functions, not for the function prototype. Reasons:
Code Example: bool does_the_universe_still_exist() { return true; } template<class T> bool frobify_two_things (const T &thing1, const T &thing2) { [...] } class Foo { bool m_alive; public: static bool is_any_foo_alive() { [...] } bool is_alive() const { return m_alive; } [...] }; 1.2 Multiline Comments
There are generally two kinds of multiline comments: documentation comments and
non-documentation comments. The following code example show that documentation
comments get an extra line, which starts the documentation comment, whereas
ordinary comments may not have an extra new line. Code Example: /** * This function is checks whether the universe still exists; however it is * unclear how a caller could call it if the universe no longer exists, so you * probably won't need it. */ bool does_the_universe_still_exist() { /* We can assume that if the program still runs, then the universe can * not possibly be gone. */ return true; } 1.3 References and Pointers are declared next to the Identifier
Reasons:
Code Example: struct Foo { char *x; string &y; Foo *z; }; void bar (Foo &foo1, const string &y) { char *x, *y, *z; const string &bar = foo1.y; } 1.4 Constructor Initializer Indentation
The initializers following a constructor should be indented like this: Code Example: class Foo : public Bar { int m_x; int m_y; vector<int> m_some_integers; public: Foo (const string &bar_name, int x, int y) : Bar (bar_name), m_x (x), m_y (y), m_z (x * y) { } [...] }; It is important that the colon (":") is placed after the constructor declaration, whereas the member initializers (or chained parent constructors) are placed on the next lines. 1.5 Data members in classes are declared before functions
Reasons:
Code Example: class AudioDistortion { double m_amount; public: void set_amount (double amount) { m_amount = amount; } void distort_block (vector<float>& block) { [...] } [...] }; 2.1 Functions, Methods, Members, Arguments and Local Variables are written as lower_case_underscore_names2.2 Type Names are written as MixedCaseNames
Code Example:
typedef float SampleType; class HashTable { [...] }; 2.3 Class Members are prefixed with m_
Reasons:
Code Example: class IntArray { vector<int> m_integers; public: IntArray (guint n_integers) : m_integers (n_integers) { } [...] } 3.1 Out Arguments of Functions that are Non Object Types are passed by Pointer, not by Reference
Reasons:
Every output argument that has a type that can be found in C (pointer, float, int, ...) may not be passed by reference, but must be passed by pointer. This rule does not apply to C++ objects which may be passed by reference. Code Example: bool parse_xyz (const string &input, float *x, float *y, float *z) { [... C++ Code that parses the input string, returns true on success and fills x, y, and z with the parsed values ... ] } |
||
|