C++ Style Guide

From Spire Trading Inc.
Revision as of 12:00, 12 February 2018 by Kman (talk | contribs) (Created page with "This article specifies the most general guidelines used for all Spire projects written using C++ as the primary programming language. Each individual project may extend or ove...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This article specifies the most general guidelines used for all Spire projects written using C++ as the primary programming language. Each individual project may extend or override the guidelines specified herein. The overall aim of this guideline is to provide consistency across the numerous projects developed by Spire in order to promote and benefit from modern C++ practices as well as tailor them to our specific requirements.

In general, the guidelines are very specific and opinionated. This is done intentionally to ensure that every detail has been given proper consideration and that every line of code is written with care and diligence. It is taken as a prerequisite that writing robust, clean and maintainable code requires paying close attention to even the smallest of details. As such, when there are two or more ways to write or express any given statement or expression, be it using spaces or tabs, number of characters per line, purple or violet, the guideline will often require that one particular approach be used to the exclusion of others.

Finally, this document is constantly evolving and as such older projects may not be up-to-date with the guidelines found here. In those circumstances one must use their best judgment about how to integrate these guidelines into older codebases. As a general principle, consistency should favor the local over the global, that is it is more important to be consistent with a function definition than within a file, and more important to be consistent within a file than within a directory, project, etc...

Core Guidelines

This document should be seen as an extension to the Cpp Core Guidelines authored and maintained by Herb Sutter and Bjarne Stroustrup. After reviewing these guidelines one should then become familiar with the Cpp Core Guidelines as documented here:

https://github.com/isocpp/CppCoreGuidelines

Those guidelines specify the best practices for writing modern C++ code. In situations where there is a conflict between the Cpp Core Guidelines and the guidelines set forth in this document, this document takes precedence. For all known situations where a conflict exists, this document should explicitly indicate that conflict to avoid confusion.

Development Environment

The two primary development environments supported by Spire projects are Ubuntu Server 16.04 LTS and Windows 10. On Unix like systems the compiler of choice is g++ 5.4 and on Windows 10 it's Microsoft Visual Studio 2017 15.5.5. In order to support these two platforms, CMake is used to produce the appropriate project/make files for each platform.

For source control, git is used and projects are to be hosted on Spire Trading's Github page.

Each developer is welcome to use an editor of their choice. Visual Studio 2017 is typically used for Windows and Visual Studio Code is typically used on Linux and Mac.

File Names

Use snake case using all lower case letters for files and directories. The main exception is for CMake files which must use the name "CMakeLists.txt"

Header files should use the extension ".hpp"

Source files should use the extension ".cpp"

Directory Structure

A project is typically broken down into a library component and an application component. Assuming a project named chat consisting of applications chat_server and chat_client sharing code common to both applications, the following directory structure should be used:

chat_project                       # Root level directory.
  \applications                    # Contains all applications.
    \chat_server                   # Contains the chat server application.
      \build                       # Files/scripts used to build the chat server.
        \config                    # Contains all CMake build files.
          \chat_server             # Contains CMake build files for the main executable.
            \CMakeLists.txt        # The CMake config file for the executable.
          CMakeLists.txt           # The CMake config file for the overall application.
        \make                      # Contains scripts to build on POSIX systems.
          \build.sh                # Script used to build the application.
          \run_cmake.sh            # Script used to produce the make/build files.
          \set_env.sh              # Script defining the environment variables.
          \version.sh              # Script that produces the VERSION stamped header file.
        \windows                   # Contains scripts to build on Windows.
          \build.bat               # Script used to build the application.
          \run_cmake.bat           # Script used to produce the VS solution.
          \set_env.bat             # Script defining environment variables.
          \version.bat             # Script that produces the VERSION stamped header file.
      \source                      # Contains the C++ source files.
        \chat_server               # Contains the file defining the main function.
          \main.cpp                # Defines the main function.
        \source_dir_a              # Contains additional source files specific the the server.
          \source_a.cpp            # C++ source code files.
          \source_b.cpp
          \source_c.cpp
        \source_dir_b              # Contains additional source files specific the the server.
          \source_d.cpp
          \source_e.cpp
          \source_f.cpp
    \chat_client                   # Contains the chat client application.
      \...                         # The structure is similar to the server application.
  \build
    \make
      \build.sh
      \local_build.sh
      \run_cmake.sh
      \setup.sh
    \windows
      \build.bat
      \run_cmake.bat
      \setup.bat
  \library
    \build
      \config
        \CMakeLists.txt
        \dir_a
          \CMakeLists.txt
        \dir_b
          \CMakeLists.txt
      \make
        \build.sh
        \run_cmake.sh
        \set_env.sh
      \windows
        \build.bat
        \run_cmake.bat
        \set_env.bat
    \include
      \chat_project
        \chat_project
          \chat_project.hpp
        \dir_a
          \a_header_1.hpp
          \a_header_2.hpp
          \a_header_3.hpp
        \dir_a_tests
          \a_header_1.hpp
          \a_header_2.hpp
          \a_header_3.hpp
        \dir_b
          \b_header_1.hpp
          \b_header_2.hpp
          \b_header_3.hpp
    \source
      \chat_project
        \dummy.cpp
      \dir_a
        \dummy.cpp
      \dir_a_tester
        \a_header_1_tester.cpp
        \a_header_2_tester.cpp
        \a_header_3_tester.cpp
        \main.cpp
      \dir_b
        \dummy.cpp
      \dir_b_tester
        \b_header_1_tester.cpp
        \b_header_2_tester.cpp
        \b_header_3_tester.cpp
        \main.cpp