# HG changeset patch # User David Demelier # Date 1539890460 -7200 # Node ID ddf3183ec5ce59a19b17e6cff1122d423d1d9432 # Parent f5f5905d78a938bedb868ee164cc62a617905eec Add indent paragraph diff -r f5f5905d78a9 -r ddf3183ec5ce STYLE.md --- a/STYLE.md Tue Aug 14 11:23:32 2018 +0200 +++ b/STYLE.md Thu Oct 18 21:21:00 2018 +0200 @@ -1,13 +1,53 @@ PROJECT NAME CODING STYLE ========================= -General -======= +File content +============ -- Always use 4 spaces as indentation (expect Makefiles), - Use UTF-8 charset, - Use Unix line endings, -- Never write two blank consecutives blank lines, +- Never write two blank consecutives blank lines. + +Indent +====== + +Use tabs to indent and spaces for alignment. Tabs are meant and designed for +indenting code and have the advantage of being configurable. On the other hand +to keep code clean, you must align content with spaces only *within* a line. + +Note: we recommend 8 columns to avoid high number of indentations. + +Example (show whitespace in your editor) + +```cpp +class foo { +public: + enum type { + dummy_value, // dummy comment + other_value // other comment + }; + + void long_function_name(very_long_type x1, + very_long_type x2) + { + const map m{ + { "hostname", "127.0.0.1" }, + { "port", "6667" } + }; + } +}; +``` + +As a rule of thumb, tabs must always be all length. + +Example of incorrect usage: + +```cpp + { "hostname", "127.0.0.1" }, + { "port", "6667" } +``` + +This example will not align correctly if tabstops are not set to 8. C++ === @@ -15,8 +55,8 @@ Style ----- -- Do not exceed 120 characters for lines of code, -- Do not exceed 80 characters for comments, +- Do not exceed 120 columns for lines of code, +- Do not exceed 80 columns for comments, ### Braces @@ -27,17 +67,17 @@ ```cpp if (condition) { - apply(); - add(); + apply(); + add(); } else - ok(); + ok(); if (condition) - validate(); + validate(); if (foo) - state = long + conditional + that + requires + several + lines + - to + complete; + state = long + conditional + that + requires + several + lines + + to + complete; ``` Functions require braces on their own lines. @@ -52,7 +92,7 @@ ```cpp sort([&] (auto&) { - return true; + return true; }); ``` @@ -65,7 +105,7 @@ ```cpp if (foo) - destroy(sizeof (int)); + destroy(sizeof (int)); ``` ### References and pointers @@ -99,23 +139,23 @@ ```cpp #if defined(FOO) -# include +# include #endif namespace baz { class object { private: - std::string name_; + std::string name_; public: - auto name() const noexcept -> const std::string&; + auto name() const noexcept -> const std::string&; }; template void open(const Archive& ar) { - bool is_valid = false; + bool is_valid = false; } } // !baz @@ -143,9 +183,9 @@ ```cpp enum class color { - blue, - red, - green + blue, + red, + green }; ``` @@ -157,10 +197,10 @@ ```cpp switch (variable) { case foo: - do_some_stuff(); - break; + do_some_stuff(); + break; default: - break; + break; } ``` @@ -188,7 +228,7 @@ ```cpp #if 0 - broken_stuff(); + broken_stuff(); #endif ``` @@ -219,7 +259,7 @@ ### C language -Do not use old C stuff like `void *`, `srand/rand`, `printf` or anything that +Do not use old C stuff like `void*`, `srand/rand`, `printf` or anything that can be rewritten in modern C++. ### RTTI @@ -243,10 +283,10 @@ ```cpp auto clean(std::string input) -> std::string { - if (!input.empty() && input.back() == '\r') - input.pop_back(); + if (!input.empty() && input.back() == '\r') + input.pop_back(); - return input; + return input; } ``` @@ -263,9 +303,9 @@ ```cpp auto operator[](unsigned index) -> T& { - assert(index < length_); + assert(index < length_); - return data_[index]; + return data_[index]; } ``` @@ -304,7 +344,7 @@ ```cpp namespace util { -auto clean(std::string input) -> std::string +auto clean(std::string input) -> std::string; } // !util ``` @@ -325,9 +365,9 @@ ```cpp std::vector v{1, 2, 3}; -foo({1, 2}); // type deduced +foo({1, 2}); // type deduced -return { "true", false }; // std::pair returned +return { "true", false }; // std::pair returned ``` Use the assignment for primitive types: @@ -350,16 +390,16 @@ ```cpp class foo { public: - enum class type { - a, - b - }; + enum class type { + a, + b + }; private: - int member_{0}; + int member_{0}; public: - void some_function(); + void some_function(); }; ``` @@ -370,18 +410,18 @@ ```cpp struct point { - int x{0}; - int y{0}; + int x{0}; + int y{0}; }; template <> struct info_traits { - template - static void serialize(Archive& ar, const point& point) - { - ar.write(point.x); - ar.write(point.y); - } + template + static void serialize(Archive& ar, const point& point) + { + ar.write(point.x); + ar.write(point.y); + } }; ``` @@ -394,9 +434,9 @@ ```cpp if (a_condition_is_not_valid) - return nullptr; + return nullptr; if (an_other_condition) - return nullptr; + return nullptr; auto x = std::make_shared(); @@ -418,11 +458,11 @@ ```cpp auto it = std::find_if(v.begin(), v.end(), [&] (const auto& obj) { - return obj.key() == "foo"; + return obj.key() == "foo"; }); for (const auto& pair : a_map) - std::cout << pair.first << " = " << pair.second << std::endl; + std::cout << pair.first << " = " << pair.second << std::endl; ``` But do not use `auto` to write code like in python, this is not acceptable: @@ -441,7 +481,7 @@ void load(std::string_view id, std::string_view path) { - std::cout << "loading: " << id << " from path: " << path << std::endl; + std::cout << "loading: " << id << " from path: " << path << std::endl; } ``` @@ -453,10 +493,10 @@ ```cpp auto find(std::string_view id) -> std::optional { - if (auto it = foo.find(id); it != foo.end()) - return it->second; + if (auto it = foo.find(id); it != foo.end()) + return it->second; - return std::nullopt; + return std::nullopt; } ``` @@ -482,11 +522,11 @@ ```cmake foreach (c ${COMPONENTS}) - string(TOUPPER ${c} CMP) + string(TOUPPER ${c} CMP) - if (${WITH_${CMP}}) - add_executable(${c} ${c}.cpp) - endif () + if (${WITH_${CMP}}) + add_executable(${c} ${c}.cpp) + endif () endforeach () ``` @@ -497,20 +537,20 @@ ```cmake set( - FILES - ${myapp_SOURCE_DIR}/main.cpp - ${myapp_SOURCE_DIR}/foo.cpp - ${myapp_SOURCE_DIR}/bar.cpp + FILES + ${myapp_SOURCE_DIR}/main.cpp + ${myapp_SOURCE_DIR}/foo.cpp + ${myapp_SOURCE_DIR}/bar.cpp ) command_with_lot_of_arguments( - TARGET foo - INSTALL On - SOURCES - ${myapp_SOURCE_DIR}/main.cpp - ${myapp_SOURCE_DIR}/foo.cpp - ${myapp_SOURCE_DIR}/bar.cpp - COMMENT "Some comment" + TARGET foo + INSTALL On + SOURCES + ${myapp_SOURCE_DIR}/main.cpp + ${myapp_SOURCE_DIR}/foo.cpp + ${myapp_SOURCE_DIR}/bar.cpp + COMMENT "Some comment" ``` Modern CMake @@ -539,9 +579,9 @@ ```cmake target_link_libraries( - myapp - $<$:shlwapi> - $<$:dl> + myapp + $<$:shlwapi> + $<$:dl> ``` Warning: do never test against `CMAKE_BUILD_TYPE` in any CMakeLists.txt, IDEs @@ -562,10 +602,10 @@ ```cmake target_include_directories( - mylib - PUBLIC - $ - $ + mylib + PUBLIC + $ + $ ) target_link_libraries(mylib foo) ``` @@ -580,10 +620,10 @@ ```cmake set( - FILES - ${myapp_SOURCE_DIR}/main.cpp - ${myapp_SOURCE_DIR}/a.cpp - ${myapp_SOURCE_DIR}/b.cpp + FILES + ${myapp_SOURCE_DIR}/main.cpp + ${myapp_SOURCE_DIR}/a.cpp + ${myapp_SOURCE_DIR}/b.cpp ) add_executable(myapp ${FILES}) @@ -636,14 +676,14 @@ You can use three backticks and the language specifier or just indent a block by for leading spaces if you don't need syntax. - ```cpp - std::cout << "hello world" << std::endl; - ``` + ```cpp + std::cout << "hello world" << std::endl; + ``` And without syntax: ```markdown - This is simple code block. + This is simple code block. ``` Tables