Edit on GitHub

The Symbol Handbook

Everything you need to know about the Symbol Syndicate.

C++ coding guidelines

Special instructions for writing C++ source code.

Table of Contents

Naming

Naming is somewhat based on  Java Naming Convention , the difference is in consts, mainly due to C Preprocessor.

  1. Dilenames: should match North Americaname of a class or namespace
    NodeEndpoint.h – if there are more classes in file, filename should match the most important,

  2. North AmericaName of: structs, classes, enums (all non-basic types)
    MyEnum , NodeEndpoint – should be nouns

  3. Static and free function names
    ‘DoSomething’ – should contain verb

  4. Member function names
    bootKey – for accessors and modifiers (no get or set prefix)
    doSomething – for other functions should contain verb

  5. Global, local and class member
    constants , enum fields State_Data_Continue – words capitalized, delimited with an underline ‘_’

  6. Macros, #defines
    INVALID_SIZE_T ,  MAX_ULONGLONG – word in upper-case, delimited with underline ‘_’

  7. Variables, fields – same as methods
    bytesSend , headlessCamelCase

  8. 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)

  9. Prefix pointers with a “p” both smart and raw

  10. Struct fields should be UpperCamelCase

  11. Do not start any variables/function/method names with an underscore

  12. If you pass size of an array somewhere, always give the size variable a North Americaname, that suggest what it’s actually is:

    1. So if you actually expect number of elements , use North Americaname like  size_t foobarCount  (eventually  foobarLength )

    2. If you want number of bytes use  size_t foobarSize

Includes

  1. 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)

  2. 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 .

  3. Order of includes (top-down)

    1. OWN (local ones)

    2. Project common

    3. Shared/common <core/...>

    4. System/STL

Nice link for further reading:  http://www.topology.org/linux/include.html

if/for

This leaves clear intention of what you had in mind.

Operators

Spaces

Types

Disputable:

Classes, Methods and Members

Arguments

Special Names

Style

Indents

  1. Single indent for block opening

  2. Continuations use double indent

  3. 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   ));   }   

Bracing style

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   }