-
Notifications
You must be signed in to change notification settings - Fork 795
Blockchain Code Style
theoreticalbts edited this page May 16, 2018
·
7 revisions
- 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 overwriteoutput
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 arePascalCase
. Preprocessor macro definitions areUPPERCASE
. - 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.
- 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.
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.