Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Coding Style Guide

Stefan deBruyn edited this page Sep 9, 2019 · 2 revisions

We adhere to no published style guide, but have instead adopted our own that we believe maximizes clarity and aesthetic.

C++

  • 80 character line limit, soft tabbing, two spaces per tab
  • .h header extension and .cpp source extension
  • PascalCase classes and typedefs, snake_case methods and variables
  • Use prefixes k_, m_, and g_ to indicate parameter, member, and global variables, respectively
  • Constants are all caps but their prefixes are not (e.g. g_SL_GRAVITY)
  • _t suffix for typedefs
  • Contextualize methods with :: or this-> wherever possible
/**
 * Particularly large or complex header files should begin with a docblock
 * detailing their purpose.
 *
 * Always define-guard headers.
 */
#ifndef EXAMPLE_DEFINE_GUARD_H
#define EXAMPLE_DEFINE_GUARD_H

/**
 * When possible, system imports precede local imports with an empty line
 * separator, and each import list is sorted alphabetically.
 */
#include <math.h>

#include "airbrake_controller.h"

typedef struct {
  double w;
  double x;
  double y;
  double z;
} Quaternion_t;

unsigned int g_bno055_pin = 0x01;
const float g_SL_ALTITUDE = 1293.876;

/**
 * Example function declaration and docblock.
 *
 * @param  k_buffer Description of argument.
 * @param  k_len    Description of argument.
 *
 * @ret    Description of return value.
 */
bool write_telemetry_packet(char* k_buffer, unsigned int k_len);

/**
 * Be stylish with your argument-heavy methods. Only when you get funky with
 * your formatting do opening braces get their own line.
 */
float compute_drag_force(float k_rho,
                         float k_cd,
                         float k_a_ref,
                         float k_vel)
{
  return 0.5 * k_rho * k_cd * k_a_ref * k_vel * k_vel;
}

/**
 * Example class declaration. We aren't doing any special formatting here, so
 * the opening brace shares a line with the code before it.
 *
 * Also, don't actually declare your class entirely within a header file
 * (except for in some templating cases).
 */
class PDController {
private:
  const float m_KP;
  const float m_KD;
  float m_err_last;
  float m_t_last;

  static float safe_compute_d(float k_derr, float k_dt) {
    return k_dt > 0 ? k_derr / k_dt : 0;
  }

public:
  PIDController(float k_kp, float k_kd) : m_KP(k_kp), m_KD(k_kd) {
    m_err_last = 0;
    m_t_last = NAN;
  }

  float update(float k_err, float k_t) {
    float res = m_KP * k_err + PDController::safe_compute_d(k_err - m_err_last,
                                                            k_t - k_t_last);
    m_err_last = k_err;
    m_t_last = k_t;
    return res;
  }
};

#endif

Python

Python code should comply with the PEP 8 standard.

Optionally, though highly recommended, Blacken your Python code.

Clone this wiki locally