Naming is somewhat based on Java Naming Convention, the difference is in constants, mainly due to C Preprocessor.
Filenames: should match name of a class or namespace
NodeEndpoint.h
– if there are more classes in file, filename should match the most important
Name of: structs, classes, enums (all non-basic types)
MyEnum
, NodeEndpoint
– should be nouns
Static and free function names
‘DoSomething’ – should contain verb
Member function names
bootKey
– for accessors and modifiers (no get or set prefix)
doSomething
– for other functions should contain verb
Global, local and class member
constants, enum fields State_Data_Continue
– words capitalized, delimited with an underline ‘_’
Macros, #defines
INVALID_SIZE_T
, MAX_ULONGLONG
– word in upper-case, delimited with underline ‘_’
Variables, fields – same as methods
bytesSend
, headlessCamelCase
Prefix class members/fields with an m_
(I actually haven’t been following that rule for a long time, but it makes reviewing the code much faster – especially when looking at commits, not within an IDE)
Prefix pointers with a “p” both smart and raw
Struct fields should be UpperCamelCase
Do not start any variables/function/method names with an underscore
If you pass size of an array somewhere, always give the size variable a name, that suggest what it’s actually is:
So if you actually expect number of elements, use name like size_t foobarCount
(eventually foobarLength
)
If you want number of bytes use size_t foobarSize
Always use “/” in includes and NEVER “”, (C standard WG14/N1256 point 6.4.7, C++ standard ISO/IEC 14882:2003 point 2.8, C++ standard ISO/IEC 14882:2011 (from working draft N3225) point 2.9)
Number of include files in header file should be minimal, that is: ONLY, that what’s actually needed in .h
What’s needed in .cpp
file should only be included in .cpp
.
Order of includes (top-down)
OWN (local ones)
Project common
Shared/common <core/...>
System/STL
Nice link for further reading: http://www.topology.org/linux/include.html
Do not use such a construct when for
doesn’t have a body
for (a; b; c);
Instead use
for (a; b; c) {}
This leaves clear intention of what you had in mind.
In case of operators please put additional space before and after them. This makes code much more readable.
This should be always used in case of =
, ==
, !=
, &&
, ||
. So this one’s ok:
for (size_t j = 0; j < foo.size() - x * 4; ++j)
While this one is not:
for (size_t j=0; j<foo.size()-x*4; ++j)
Always put a space after semicolon ‘;’ in for, that is ok:
for (size_t j = 0; j < foo.size() - 1; ++j)
This one’s not:
for (size_t j = 0;j < sections.size();++j)
Always put a space after coma ‘,’ in function args, like:
outputAsciiString(buffer, something, elsewhere);
Not:
outputAsciiString(buffer,something,elsewhere);
Do NOT leave whitespaces at line-endings (here’s a regex for “Quick Replace” in visual studio: [ ]+$
)
size_t
should be used whenever dealing with data size (in many cases auto
is fine too):
The result of sizeof()
is size_t
Difference between pointer types is always size_t
Index of an element in an array should be of size_t
type
The result of strlen()
should usually be size_t
Most STL containers uses size_t
as default size, count, length and index type
Please use types defined in stdint.h (uint8_t
, uint16_t
, uint32_t
, etc.)
Disputable:
Please avoid using signed types and signed math unless it’s really necessary and reasonable.
Classes should be named using CamelCase
(first letter capital)
Class order (disputable):
Private constants (as they are usually used early)
Public constants
Methods (if possible public, protected, private)
Fields
Public members (should probably be used only for POD types)
Protected members
Private members
Avoid passing arguments as pointers (reference is always preferred) unless it’s really intended and needed.
Use const
references or const
types when possible.
BlockChain not Blockchain
Timestamp not TimeStamp
Filesystem not FileSystem
configuration
for class names
config
for variable names
Single indent for block opening
Continuations use double indent
Initializer list, and ctors/function/method arguments, have double indent
Example 1.
for (auto&& pEntity : entities) {
singleEntityVector[0] = pEntity;
auto result = dispatcher.dispatch(m_config.ValidationPolicy, singleEntityVector);
m_config.pObserver->notify(*pEntity, observerContext);
}
Example 2.
CATAPULT_LOG(debug) << "comparing chain scores: " << localScore << " (local) vs "
<< remoteScore << " (remote)";
return pState
&& pState->ImportanceInfo.Importance > Importance(0)
&& pState->Balances.get(Xem_Id) >= minHarvestingBalance;
Example 3.
// mind the double indent for method arguments
static thread::future<std::unique_ptr<model::Block>> BlockAt(
Height height,
const io::BlockStorageView& storage) {
if (Height(0) == height || storage.chainHeight() < height) {
auto exception = CreateHeightException("unable to get block at height", height);
return thread::make_exceptional_future<std::unique_ptr<model::Block>>(exception);
}
return thread::make_ready_future(storage.loadBlock(height));
}
empty body, short
Foo() : m_value(0)
{}
empty body, long
// two indents
Foo(very arguments, much wow)
: m_value(0)
, m_xman(professor)
{}
body, short
Foo() : m_value(0) {
// body
}
body, long
// two indents
Foo(very arguments, much wow)
: m_value(0)
, m_xman(professor) {
// body
}
Last updated by Xavi Artigas
on 2021‑11‑09.