Skip to content

Blockchain Code Style

theoreticalbts edited this page May 16, 2018 · 7 revisions

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.
  • Prefer code sharing and avoid code copies
  • Use private/protected members to limit access to class contents and simplify its maintenance
  • Body of do / while loops should always be followed by brackets.
  • 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.

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.

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.

Clone this wiki locally