Coding Guidelines

General Guidelines

Choosing Good Names

  1. Use complete descriptive names with mixed case like firstName or PurchaseOrder (but avoid unnecessarily long names).
  2. Short names, like i or k2, should only be used for counter variables and index variables.
  3. Use abbreviations sparingly, but consistently, based on a list of standard abbreviations (e.g. "No" for "Number"). Use mixed case also for acronyms:e.g. BtuStudent instead of BTUStudent.
  4. Avoid names that are too similar or differ only in case: for instance, myCar and mycar are too similar.
  5. Use upper case for the names of constants, using the underscore for separating the different parts of a composite name, like in MIN_VALUE or DEFAULT_START_DATE.
  6. The names of variables and parameters should begin with a lower case letter, as in car1 or carType.
  7. Use the plural form for the names of list variables (or parameters), such as phoneNumbers or items.
  8. For procedure and function names, you should choose an active verb as the first part of the name, which is normally followed by a noun phrase, such as drawRectangle or openAccount.

Program Layout

  1. The unit of indentation for code blocks is two spaces. Do not use tabs.
  2. A program line should contain only one statement and should not be longer than 80 characters.
  3. If a statment does not fit on a single line, it may be necessary to break it. Place the break after an operator, ideally after a comma, and indent the next line by 4 spaces.
  4. Forming sections of code: within a code block you should use empty lines or comment lines for designating new processing steps.
  5. Use spaces around operators and after parenthesis and commas.
  6. The code of a procedure or function should always fit on a screen, so it should not have more than 30 lines.


  1. Use line comments (typically preceeded by "//") for explaining the meaning of variables and statements that require explanation, either appended to the statement or preceeding the line.
  2. Do not explain what is obvious, as in 
    i = i+1;  // increment i by 1


  1. Use formal documentation comments, between /** and */, for documenting methods, classes and code files.
  2. Your documentation text should be simple and short.
  3. Write the documentation before you write the code.
  4. Document why something is being done, and not just what.

JavaScript Guidelines


All variables have to be declared before used.The var statements should be the first statements in the function body. The use of global variables should be minimized.

Function Declarations

All functions should be declared before they are used. Inner functions should follow the var statement. This helps make it clear what variables are included in its scope.

There should be no space between the name of a function and the left parenthesis of its parameter list. There should be one space between the right parenthesis and the left curly brace that begins the statement body. So, a function declaration should exactly look like the following example:

function outer( c, d) {
  var e = c * d;

  function inner( a, b) {
    return (e * a) + b;

  return inner(0, 1);

If a function literal is anonymous, there should be one space between the word function and the left parenthesis, as in the following examples:

div.onclick = function (e) {
  return false;

myObject = {
  myMethod: function () {
    return this.myAttribute;
  myAttribute: 0


Put a ; (semicolon) at the end of every simple statement. Note that an assignment statement which is assigning a function literal or object literal is still an assignment statement and must end with a semicolon.

JavaScript allows any expression to be used as a statement. This can mask some errors, particularly in the presence of semicolon insertion. The only expressions that should be used as statements are assignments and function invocations.

A return statement with a value should not use ( ) (parentheses) around the value. The return value expression must start on the same line as the return keyword in order to avoid semicolon insertion.

The if class of statements should have the following form:

  if (condition) {
  if (
condition) {
  } else {
  if (
condition) {
  } else if (
condition) {
  } else {

A switch statement should have the following form:

  switch (expression) {

Each group of statements (except the default) should end with break, return, or throw.

Use {} instead of new Object(). Use [] instead of new Array().

Always use the === and !== operators instead of the == and != operators, which do type coercion.