CodeStyle

Code Style

This repository maintains standards for code style for ease of use and readability. Please ensure that your contributions follow these guidelines. Our languages of choice for the core library are JavaScript, CoffeeScript, and NE Script (files whose extensions begin with .ne_).

Global Styling (regardless of language)

  • 2 spaces per indent level (except in C++ with 4 spaces per indent).
  • Single quote ' convention for strings.
    • Exceptions differ for .js and .ne_*, as well as .coffee:
    • Double quote " when the string contains ' so we avoid this: '\''.
    • JS/NE: Backtick ` when both ' and " appear in the string, or using templates `${expr}`.
    • Coffee: """" for when the string contains " or using templates "#{expr}". Backticks ` do not apply in coffeescript; quote them normally with ' and " as needed.
  • No more than 140 characters per line. Ideally, entire functions should fit on your screen at once with a normal font size.
  • Object literals should not have unnecessarily quoted or bracketed property keys.

    Do: const o = { a: 1, 1: 'value', [expr]: variable };

    Don't: const o = { 'a': 1, '1': 'value', [1 + 1]: variable };

Additional Javascript Styling

  • One-line lambdas should not have braces around function body. Explicitly return undefined if that behaviour is desired.

    Do: const fn = () => 1;

    Don't: const fn = () => { return 1 };

  • Lambdas with one parameter should not have parens around the param, ever.

    Do: const fn = a => 1; const fn2 = b => { etc(); etc(); return b; }

    Don't: const fn = (a) => 1; const fn2 = (b) => { ... }

  • All (standard) lines of code must be terminated with a `;`. Function definitions with lambda must also be terminated with `;`.

    Do: expr; const fn2 = b => { etc(); etc(); return b; };

  • Multi-line Arrays and Objects should have trailing commas.

    const a = [
     1,
     2, // <- this comma
    ]

    Don't: const o = { 'a': 1, '1': 'value', [1 + 1]: variable };