Skip to content

Javascript coding guidelines

Henrik Lievonen edited this page May 2, 2014 · 2 revisions

This document defines some coding guidelines for Eppabasic. Guidelines are based on Felix Geisendörfer's javascript guidelines.

Indention

Use 4 spaces for indenting the code.

Newlines

Use UNIX-newlines (\n).

Semicolons

Use semicolons after every statement and assignment.

Quotes

Use single quotes (') for string literals.

Braces

Opening braces go on the same line as the statement. Closing braces go to a new line and are indented as the original statement.

Declaring variables

Declare only one variable per var.

Naming conventions

Use lowerCamelCase for variables, properties and function names. Use UpperCamelCase for class names. Use UPPERCASE for constants.

Arrays

Define short arrays on the same line as the var. For long array definitions use trailing slashes.

Objects

One item per line. Use trailing slashes. Use quotes on keys only when needed.

Comparision

Use alway either === or !== for comparing stuff.

Closures

Name closures with descriptive names

Comments

Use slashes for comments, alos multiline. Comment only to explain the code at higher levels.

Documentation and classes

Use /**/ for documenting functions and classes.

/*
 * Allows editing code with autocorrecting editor
 */
function Editor() {
  // ...
}

Editor.prototype = {
  /*
   * Inserts text at spesified position
   */
  insert: function insert(pos, text) {
    if (typeof pos === 'string') {
      text = pos;
      pos = // ...
    }
    // ...
  },
  /*
   * Selects text
   */
  select: function select(start, end) {
    // ---
  }
};
Clone this wiki locally