Skip to content

Blockchain Code Style

Łukasz Bujak edited this page May 17, 2018 · 7 revisions

theoreticalbts

Suggestions

  • Pass by const reference, not pointer, whenever possible.
  • Input/output function arguments should use references, for example void do_something( const std::string& input, std::string& output ) will overwrite output as a side-effect.
  • Body of do / while loops should always be followed by brackets.

Spaces

  • Use spaces, not tabs.
  • Indentation size is 3 spaces.
  • UNIX-style newlines (LF)
  • No trailing whitespace (configure your editor/IDE to remove it automatically)
  • All files should end with a newline character.

mvandeberg

Suggestions

  • use private/protected members to limit access to class contents and simplify its maintenance
  • Branching and looping statements are always followed by brackets.
  • No trailing whitespace (configure your editor/IDE to remove it automatically). The exception to this rule is that all files must end with a single new line character.
  • All namespaces, class/struct name, methods, and variables are snake_case with private member variables prefixed with an underscore _. Template parameters are PascalCase. Preprocessor macro definitions are UPPERCASE.
  • Never use the built in integer types (short, int, long, etc.). Use explicitly sized types (int16_t, int32_t, int64_t, etc.).
  • Add explicit parenthetical statements to compound boolean expressions to enhance readability and avoid all potential undefined behavior.
  • All header files begin with #pragma once. This stack overflow conversation has great insight on whether to use pragmas or include guards. https://stackoverflow.com/a/34884735 While this particular argument is against the use of #pragma once, we build with CMake which disambiguates headers of the same name.

Notes

The Google C++ Style Guide has good recommendations. I do not agree with all recommendations in this guide, but I do believe it is a solid foundation for any C++ developer.

vogel76

Suggestions

  • Pass by const reference, not pointer, whenever possible. If you need to operate on non-const object, better use pointer than reference to avoid confusion during evaluating variable assignment i.e.:
    account_object& oneAccount = _db.get<account_object>(name1) account_object& secondAccount = _db.get<account_object>(name2); /// Consider you need to replace variables for some reason (just replace reference values, not objects itself) secondAccount = oneAccount; /// Here **object contents** is rewritten, not just references
  • inout/output function arguments declare as pointers, i.e.:
    void doSomething(const std::string& input, std::string* output); /// At caller side is visible difference that 2nd argument can be modified inside function: std::string out; doSomething("ABC", &out);
  • Prefer code sharing and avoid code copies
  • use private/protected members to limit access to class contents and simplify its maintenance

youkaicountry

Indentation

Use spaces, not tabs for indentation. The indent size is 3 spaces.

Suggestions

-- Pass by reference, not pointer, whenever possible.