Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Style Guide

Jeff Dyer edited this page Jan 5, 2014 · 13 revisions

Style Guide

Note: This wasn't always followed, but any new code should do this.

General

  • Indentation - 2 spaces
  • Line Length - 80 characters
  • Tab characters - not used to format code
  • Encoding - UTF-8 with LF as line terminator (Windows system must be configured to not use CR/LF)

Naming

  • variables and functions - lowerCamelCase
  • constructor like functions - UpperCamelCase
  • constants - ALL_UPPER_CASE_WITH_UNDERSCORES

Braces

  • Always use braces and put them on same line even for single line control statements
if (someVar) {
  return true;
} else {
  return null;
}

Semicolon

Semicolons must be always added as statement terminators.

Trailing Comma in Array and Object Literals

Use a trailing comma in an Array initializer ([one, two, three, ]) or object initializer ({one: 1, two: 2, three: 3, }) when the initializer represents a literal list of items that may grow or shrink overtime.

The motivation for this guideline is two fold:

  • It avoids syntax errors due to copying the last item to extending the list.
  • It signals to the user that this list is safe to extend.

A note to the concerned: [1, 2].length === [1, 2,].length

White Space

  • Space after control statements (if, else, while, for, ...)
if (someVar) {

Equalities

  • Use only strict equalities (and inequalities) in control statements, e.g.
if (someVar === conditionA) {
  return true;
} else if (someVar !== conditionB) {
  return false;
}

Classes

The standard way of creating classes in Shumway is the following. Please note that by class we mean an object that is class-like. Also, note the naming of all anonymous functions.

var ClassName = (function () {
  function constructor(...) {
    ...
  }

  constructor.prototype = {
    functionName: function functionName(...) {
      ...
    }
  };

  return constructor;
})();

Error Handling / Assertions

  • Use the available error handling functions:
unexpected("..");
notImplemented("..");
  • Guard assertions using:
release || assert(condition, "...");