This document defines the coding style for this C++ project. The goal is readability, consistency, and safety.
- Prefer clarity over cleverness
- Be consistent with existing code
- Optimize only when needed and measured
- Follow modern C++ (C++17/C++20) best practices
- Headers:
snake_case.hpp - Sources:
snake_case.cpp - One primary class per file (when reasonable)
Example:
order_book.hpp
order_book.cpp
#include <system_headers>
#include <third_party_headers>
#include "project_headers"- Classes / Structs / Enums: PascalCase
class OrderBook;
struct TradeEvent;
enum class OrderSide;- Functions & methods: snake_case
void update_order_book();
bool is_valid() const;- Local variables: snake_case
- Member variables: snake_case_ (trailing underscore)
int order_count;
std::string symbol_;constexprorconst- Upper snake case
constexpr int MAX_RETRIES = 5;- 4 spaces
- No tabs
- K&R style
if (condition) {
do_something();
}- Max 100 characters per line
- Prefer
constexpr,noexcept,[[nodiscard]] - Prefer
enum classoverenum - Prefer
std::array/std::vectorover raw arrays - Avoid raw
new/delete
[[nodiscard]] int compute() noexcept;- Use references when ownership is not transferred
- Use smart pointers for ownership
void process(const Order& order);
std::unique_ptr<Session> session;- Prefer return values or
std::expected(if available) - Use exceptions only for truly exceptional cases
- Never throw from destructors
- Mark everything
constwhere possible
const std::string& symbol() const;- Headers must be self-contained
- Use
#pragma once - Do not include unused headers
#pragma once- Explain why, not what
- Keep comments up to date
// Price is adjusted to avoid crossing the spread- No
std::coutin production code - Use project logging facilities
- Debug-only code must be guarded
#ifdef DEBUG
log_debug("state=", state);
#endifRecommended tools:
clang-formatclang-tidycppcheck
Formatting must pass:
clang-format --style=file
- C++ Core Guidelines
- Google C++ Style Guide (as reference, not strict)
- LLVM Coding Standards
Any deviation from this style must be justified and documented.