Skip to content

Code Style Guide

Peter Yee edited this page Jun 6, 2017 · 4 revisions

Caveat Lector

This is by no means an exhaustive style guide, code styles will evolve slightly and will vary from repo to repo. Please pay attention to the surrounding code and follow the patterns within them. If you want to change a style pattern across a codebase, please issue a pull request that only has whitespace/code-style changes.

Naming Conventions

Classes and Files related to classes shall begin with a capitol letter and have no underscores

Good: class ClassName SomeObject.js

Bad: class class_name someObject.h

Variables shall be all lower case and be underscored. If your language has pointers, remove some vowels to indicate. When using javascript, avoid "var" whenever possible.

Good: let some_field const triangle uint8_t *pxl this._internal_variable = {};

Bad: someField M_Triangle uint8_t *pixel

Functions shall be snake case and begin with a lower case letter. When talking about a function, always append a "()" In javascript, use es6 anonymous function calls whenever possible.

Good: let doStuff() whyTheHellNot()

let func = (arg) => {
}

Bad: do_it() NotNow return function named(arg){}

Whitespace

For indentation, python is 4 spaces, everything aside is 2 spaces. Tabs are right out, any pull requests that contain a "\t" character should be rejected for it. No trailing whitespaces. Any line should ideally be 80 chars or less, but exceptions to this rule is allowed. If statements may exclude '{}' if they are a single line, and may be on the same line. For javascript, one space between ')' and '{' characters, no newline.

Good:

let thing = {
  field: "whatever",
};

let thing = {less_than: 80, chars: true};

let functionName() {
  return stuff;
}

if(err) return callback(err);

Bad:

   let thing = {if_this_takes_up_way_more_than_just_80_characters_then_dont_put_it_on_a_single_line: true};

const any_trailing_space

let functionName()
{
  return null;
}

class Thing{

  if(err){
    return callback(err);
  }

Line Endings

No windows-style endlines allowed for any reason. Semicolon at the end of every line unless you cannot, do it even if not needed. Comma at end of every line in an immediate object definition.

Good: doStuff();

let thing = {
  bunch: 1,
  of: 1,
  fields: 1,
}

Bad: let even_if_not_needed

const please = {
  do: 1,
  it_anyways: true
}

Clone this wiki locally