Newer
Older
Import / applications / HighwayDash / ports / Docs / Markdown / CodingStyleGuide.md

@page Guides @subpage CodingStyleGuide @page CodingStyleGuide Coding Style Guide @tableofcontents

@page CodingStyleGuide @subpage Introduction @page Introduction Introduction

A style guide should make things easier and more consistent.

  • Easier to decide how to name things.
  • Easier to format code, just follow the guide rather than need to make decisions.
  • Makes it easier to read code that follows the style.
  • Makes the code feel cohesive.
  • Makes things more discoverable if things for example follow conventions. You can guess / know what something will be, such as what a function will be called if it follows the conventions.

General Rules

  • Only one main class to a file
  • include test cases and documentation together with the code

Coding style tips

  • Each class should be in a seperate file
  • Each class should contain unit tests in the cpp file
  • Each class should contain documentation in the h file

References

refer to various books and other style guides

@page CodingStyleGuide @subpage CppVersion @page CppVersion C++ Version

Code can assume a minimum of C++11, and may be some C++14 features. C++17 may be used provided compatibility with older version can be provided.

@page CodingStyleGuide @subpage Layout @page Layout Layout

Header File Layout

  • Copyright
  • Header guard
  • Includes
  • Defines
  • Declare namespace
  • Forward declarations
  • Class declarations

Example ClassName.h:

//  BlockyFroggy
//  Copyright © 2017 John Ryland.
//  All rights reserved.

#pragma once
#ifndef CLASS_NAME_H
#define CLASS_NAME_H


#include "Base.h"


//! NameSpace description
namespace NameSpace
{


//! Class description
class ClassName : public Base
{
public:
  //! Member function description
  //! @param a_arg is an integer
  //! @return some integer value
  int memberFunction(int a_arg);
private:
  int m_memberVariable;
};


} // namespace NameSpace


#endif // CLASS_NAME_H

Source File Layout

  • Copyright
  • Includes
  • Namespace
  • Class definitions
  • Unit tests

Example ClassName.cpp:

//  BlockyFroggy
//  Copyright © 2017 John Ryland.
//  All rights reserved.

#include "ClassName.h"


namespace NameSpace {


int ClassName::memberFunction(int a_arg)
{
  if (someFunction())
  {
    m_memberVariable = 1;
    return 1;
  }
  return 0;
}


} // namespace NameSpace


// Unit Tests
#if ENABLE_UNIT_TESTS

DECLARE_UNIT_TEST(ClassNameTest)
{
  NameSpace::ClassName example;
  
  CHECK(example.memberFunction(10) == 0);  
}

#endif // ENABLE_UNIT_TESTS

@page CodingStyleGuide @subpage CopyrightHeaders @page CopyrightHeaders Copyright Headers

All files should contain a copyright header with the following fields:

  • file name
  • project name
  • copyright year, author name, all rights reserved.

@page CodingStyleGuide @subpage Headers @page Headers Header Files

Code can use #pragma once. If want to use #ifndef #define #endif, this can optionally be done also.

@page CodingStyleGuide @subpage Namespaces @page Namespaces Namespaces