View on GitHub

Graphitti

A project to facilitate construction of high-performance neural simulations

C++ design and Coding standards

C++ is the main development language used in Graphitti. Currently, the code should target C++17, the version targeted will advance over time.

The goal of this guide is to describe in detail the dos and don’ts of writing C++ features that are used in Graphitti. These rules exist to keep the code base manageable while still allowing coders to use C++ language features productively.

NOTE:

  1. The guide is developed using two standard C++ style guides -
  2. This is a living document and will be updated as Graphitti adopts new C++ features.
  3. For details on features not covered here, refer to the above two guides for the best practice. Please discuss with Professor Stiber and document the feature details here.

Contents:

  1. Use of const and constexpr
  2. Copy and Move operations
  3. Smart Pointers
  4. Aliases
  5. Inputs and Outputs
  6. Override Keyword
  7. Return-Reference from accessor methods

Use of const and constexpr

 class A { 
    const  int i = 100;

    const int * const num= &i;

    int function1(char c) const; 
    const int* function2(const char* string) const;
};
 class A { 
    constexpr  int i = 100;

    constexpr int function1(int x, int y);
};

Recommendation:

Copy and Move operations

Recommendation:

class Copyable {
 public:
  Copyable(const Copyable& other) = default;
  Copyable& operator=(const Copyable& other) = default;
};

class MoveOnly {
 public:
  MoveOnly(MoveOnly&& other) = default;
  MoveOnly& operator=(MoveOnly&& other) = default;
};

class NotCopyable{
 public:
  NotCopyable(const NotCopyable&) = delete;
  NotCopyable& operator=(const NotCopyable&)= delete;
};

class NotMovable{
 public:
  NotMovable(NotMovable&&) = delete;
  NotMovable& operator=(NotMovable&&)= delete;
};

Smart Pointers

  std::unique_ptr<int> value1(new int(10)); 

  // OR 

  std::unique_ptr<int> value2; 
  value2.reset(new int(47));

Recommendation:

Aliases

Inputs and Outputs

Override Keyword

The “override” keyword in C++ explicitly indicates that a member function in a derived class is intended to override a virtual function from a base class.

Recommendation:

2 Major advantages:

  1. A function or destructor marked override or final that is not an override of a base class virtual function will not compile, and this helps catch common errors.
  2. The specifiers serve as documentation; if no specifier is present, the reader has to check all ancestors of the class in question to determine if the function or destructor is virtual or not [Google style guide].

Return-Reference from accessor methods

Accessor methods (getters) should generally return references to the data they access instead of returning by value (excepts for primitives) to avoid unnecessary copying of objects and enable direct modification of the underlying data.

When returning references, ensure that the referenced object remains valid throughout the lifetime of the returned reference, ensuring data integrity and avoiding potential issues with dangling references.

Recommendations:


« Go back to the Coding Conventions page


« Go back to the Developer Documentation page


« Go back to the Graphitti home page