From 8a85e764ab8371f8a6e7af9f0f462cd7d1bce3ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 20 Jul 2012 14:53:26 +0200 Subject: [PATCH] Doc: updated Coding Style. * Links to LLVM and Qt guidelines * Forbidden features * Assertions * Keyword order --- doc/coding-style.dox | 54 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/doc/coding-style.dox b/doc/coding-style.dox index 41da98db2..5a06bdfbd 100644 --- a/doc/coding-style.dox +++ b/doc/coding-style.dox @@ -9,6 +9,12 @@ Please note that if you have a good excuse to either break the rules or modify them, feel free to do it (and update this guide accordingly, if appropriate). Nothing is worse than rule that hurts productivity instead of improving it. +You can also take inspiration from other thorougly written coding style +guidelines of large projects: + + - LLVM: http://llvm.org/docs/CodingStandards.html + - Qt: http://qt-project.org/wiki/Qt_Coding_Style + @section cmake CMake code All cmake functions and macros (e.g. `add_executable()`, `set()`) are @@ -225,9 +231,10 @@ switch(type) { } @endcode -@subsubsection cpp-virtual Virtual functions +@subsubsection cpp-keywords Class member and function keywords -Only base virtual functions should have `virtual` keyword, not the +Use keywords in this order: `template virtual inline constexpr static`. Only +base virtual functions should have `virtual` keyword, not the reimplementations. @subsubsection cpp-includes Includes @@ -250,6 +257,49 @@ it should be wrapped on multiple lines, each successive line indented and the line wrapping indicator `\` should be aligned around 80th column, preferably on some tab-stop. +@subsection cpp-constexpr Constant expressions and constants + +Use `constexpr` keyword as much as possible, mainly for getters and static +pure functions, e.g. Matrix4::translation(). If the function is not a single +return-statement, try hard to make it so. + +Traits class members and class constants which are not meant to be referenced +or pointed to should be defined as inline (constexpr) function, because +declaring them as static const variable will result in another (probably +unwanted) symbol. + +@subsection cpp-assert Assertions + +Use asserts as much as possible. %Corrade utility library has convenient +`CORRADE_ASSERT()` macro, which does everything needed - checks the statement, +prints given message to error output and exits immediately or returns default +value: + +@code +T operator[](size_t pos) const { + CORRADE_ASSERT(pos < size(), "Index out of range", T()) + return data[pos]; +} +@endcode + +@subsection cpp-forbidden Forbidden C/C++ features + +Don't use C-style casts, use C++-style `static_cast` and `reinterpret_cast` +instead. For numeric types (e.g. casting from int to float) you can use +"constructor cast": + +@code +int a = 22; +int b = 7; +float pi = ((float) a)/b; // bad! +float pi = static_cast(a)/b; // good +float pi = float(a)/b; // even better here +@endcode + +Don't use RTTI (`dynamic_cast` and `std::typeinfo`) and exceptions. They +violate C++ principle of "don't pay for what you don't use" and thus they are +disabled in the code (GCC/Clang option `-fno-rtti -fno-exceptions`). + @section comments Comments All comments should be in C-style (i.e. slash and asterisk, not two slashes).