Skip to content

Instantly share code, notes, and snippets.

@ritvvijparrikh
Last active December 28, 2015 12:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ritvvijparrikh/7498666 to your computer and use it in GitHub Desktop.
Save ritvvijparrikh/7498666 to your computer and use it in GitHub Desktop.

General

  • Indentation - 4 spaces (do not use tabs)
  • Avoid lines longer than 80 characters.
  • Required License in File Header

Naming

  • class - UpperCamelCase
  • functions - lowerCamelCase (preferrably verbs)
  • variables - lower_case_with_underscores (preferrably nouns)
  • constants - ALL_UPPER_CASE_WITH_UNDERSCORES

Classes

The standard way of creating classes 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 ClassNameClosure() {
  function ClassName(...) {
    ...
  }

  ClassName.prototype = {
    functionName: function ClassName_functionName(...) {
      ...
    }
  };

  return ClassName;
})();

Variables

  • Declare a variable before it is used
  • This helps identify undeclared variables that may become implied globals. Implied global variables should never be used.
  • var statements should be the first statements in the function body
  • Give each var variables it's own line and comment
  • list them in alphabetical order
  • Use of global variables should be minimized.
var currentEntry; // currently selected table entry
var level;        // indentation level
var size;         // size of table

Braces

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

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

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;
}

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

Comments

  • Liberally comment your code.
  • But avoid redundant comments e.g.
i = 0; // Set i to zero.

Further reading:

@prtksxna
Copy link

Its always nice when function names are verbs and variable names are nouns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment