Use clang-format tool to format your changes. See CONTRIBUTING for details.
-
No
using namespace
declarations in header files.// WRONG: #include <cassert> using namespace std; tuple<float, float> meanAndSigma(vector<float> const& _v); // CORRECT: #include <cassert> std::tuple<float, float> meanAndSigma(std::vector<float> const& _v);
-
Preprocessor symbols should be prefixed with the namespace in all-caps and an underscore.
-
File comment is always at top, and includes:
- Copyright.
- License.
-
Use Zilliqa Standardized Guard for header files which is given by https://google.github.io/styleguide/cppguide.html#The__define_Guard.
ZILLIQA_<path_to_header_file>_H_
-
Prefer static const variable to value macros.
-
Prefer inline constexpr functions to function macros.
GOLDEN RULE: Preprocessor: ALL_CAPS; C++: camelCase.
- Use camelCase for all class/structure names.
- All preprocessor symbols (macros, macro arguments) in full uppercase with underscore word separation.
All other entities' first alpha is lower case.
- Leading
m_
to data members. - Leading
g_
to global (non-const) variables. - Leading
s_
to static (non-const, non-global) variables.
-
{Typename} + {qualifiers} + {name}.
-
Only one per line.
-
Favour declarations close to use; don't habitually declare at top of scope ala C.
-
Always pass non-trivial parameters with a const& prefix.
-
To return multiple "out" values, prefer returning a tuple or struct. See F.21.
-
Never use a macro where adequate non-preprocessor C++ can be written.
-
Make use of
auto
whenever type is clear or unimportant:- Always avoid doubly-stating the type.
- Use to avoid vast and unimportant type declarations.
- However, avoid using auto where type is not immediately obvious from the context, and especially not for arithmetic expressions.
-
If you need to pass more than one boolean to a function, consider using an enum instead
-
Prefer
enum class
to straightenum
. -
Try to use uniform initialization syntax wherever possible.
// WRONG: const double d = 0; int i, j; char *s; float meanAndSigma(std::vector<float> _v, float* _sigma, bool _approximate); Derived* x(dynamic_cast<Derived*>(base)); for (map<ComplexTypeOne, ComplexTypeTwo>::iterator i = l.begin(); i != l.end(); ++l) {} // CORRECT: enum class Accuracy { Approximate, Exact }; double const d = 0; int i; int j; char* s; std::tuple<float, float> meanAndSigma(std::vector<float> const& _v, Accuracy _a); auto x = dynamic_cast<Derived*>(base); for (auto i = x.begin(); i != x.end(); ++i) {}
- Structs to be used when all members public and no virtual functions.
- Classes to be used in all other circumstances.
- One member per line only.
- Private, non-static, non-const fields prefixed with m_.
- Avoid public fields, except in structs.
- Use
override
,final
andconst
as much as possible. - No implementations with the class declaration, except:
- template or force-inline method (though prefer implementation at bottom of header file).
- one-line implementation (in which case include it in same line as declaration).
- For a property
foo
- Member:
m_foo
; - Getter:
foo()
; also: for booleans,isFoo()
- Setter:
setFoo()
;
- Member:
- Collection conventions:
...s
meansstd::vector
e.g.using MyTypes = std::vector<MyType>
...Set
meansstd::set
e.g.using MyTypeSet = std::set<MyType>
...Hash
meansstd::unordered_set
e.g.using MyTypeHash = std::unordered_set<MyType>
- Class conventions:
...Face
means the interface of some shared concept. (e.g.FooFace
might be a pure virtual class.)
- Avoid unpronounceable names:
- If you need to shorten a name favour a pronouncable slice of the original to a scattered set of consonants.
- e.g.
Manager
shortens toMan
rather thanMgr
.
- Avoid prefixes of initials (e.g. DON'T use
IMyInterface
,CMyImplementation
) - Find short, memorable & (at least semi-) descriptive names for commonly used classes or name-fragments.
- A dictionary and thesaurus are your friends.
- Spell correctly.
- Think carefully about the class's purpose.
- Imagine it as an isolated component to try to decontextualise it when considering its name.
- Don't be trapped into naming it (purely) in terms of its implementation.
- Prefer
using
totypedef
. E.g.using ints = std::vector<int>
rather thantypedef std::vector<int> ints
. - Generally avoid shortening a standard form that already includes all important information:
- e.g. stick to
shared_ptr<X>
rather than shortening toptr<X>
.
- e.g. stick to
- In general expressions should be roughly as important/semantically meaningful as the space they occupy.
- Use
INFO
for non-critical, informative logging messages - Use
WARNING
for logging potentially dangerous messages - Use
FATAL
for logging a message and thereafter terminating the process
Herb Sutter and Bjarne Stroustrup
- "C++ Core Guidelines" (https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md)
Herb Sutter and Andrei Alexandrescu
- "C++ Coding Standards: 101 Rules, Guidelines, and Best Practices"
Scott Meyers
- "Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition)"
- "More Effective C++: 35 New Ways to Improve Your Programs and Designs"
- "Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14"