Skip to content

Coding standards

Robbert Broersma edited this page Jun 10, 2016 · 7 revisions

JSDoc

We use a subset of JSDoc annotations, as defined by Closure Compiler. See this document for the complete syntax: Annotating JavaScript for the Closure Compiler

Short takeaways:

  • Use @return instead of @returns.
  • Use string, number and boolean instead of String, Number and Boolean.

Configure Sublime to maintain coding standards

To maintain coding standards, it is good practice to use project guidelines. For following these rules it is helpful to configure Sublime Text and make it detect coding violations, like using too much/too little whitespace for example!

  1. Open Sublime and press Cmd+Shift+P (OS X)
  2. Type install and choose Package Control: Install Package
  3. Find and select EditorConfig
  4. Find and select SublimeLinter
  5. Find and select SublimeLinter-shellcheck
  6. Find and select SublimeLinter-contrib-eslint
  7. Install eslint on your computer with sudo npm install -g eslint in Terminal
  8. Make sure you have a file names .eslintrc in the root of your project
  9. Restart Sublime and test if a violation of the code guidelines is detected (an extra space for example)

Branch names for issue branches

Using the issue number in your branch name will automatically add the commit to the issue info. We'll use your_name/issue-issue_number so for example yolijn/issue-42

For renaming a branch locally and remotely:

git branch -m old_branch new_branch         # Rename branch locally    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote
git push origin :old_branch                 # Delete the old branch    

Ordering code in modules

When defining a module:

  1. First define a constructor or namespace object.
  2. Define static properties
  3. Define static methods
  4. Define prototype properties
  5. Define prototype methods that override the parent class
  6. Define prototype methods

For example:

/**
 * @constructor
 */
function Vector()
{
    // Step 1
}

/**
 * @private
 * @const {number}
 */
Vector.__min = -Infinity; // Step 2

/**
 * @param {Array} array
 */
Vector.from = function (array)
{
    // Step 3
};

/** @type {number} */
Vector.prototype.length = 0; // Step 4

/**
 * @override
 * @return {string}
 */
Vector.prototype.toString = function()
{
    // Step 5
};

/**
 * @override
 * @return {string}
 */
Vector.prototype.clone = function()
{
    // Step 6
};

Clone this wiki locally