JS Styleguide

These are merely guidelines. They should not be adhered to mechanically, especially if a deviation would make your code more readable.

General

  1. Code SHOULD be indented with spaces only.
  2. Each level of indentation SHOULD consist of 2 spaces.
  3. Lines SHOULD be 80 characters max.

Naming

Overview

Construct Convention Examples
Pseudoclass name (i.e., constructor function) CamelCase with an initial capital letter MightyBalrog, MagicWeapon
Namespace name camelCase with an initial lowercase letter clericSpells, savingThrows
Public method camelCase with an initial lowercase letter castDimensionalDoorway, rollForInitiative
Public variable camelCase with an initial lowercase letter materialComponents, hasTrackingAbilities
Private method camelCase with an initial lowercase letter and underscore _getHealth
Private variable camelCase with an initial lowercase letter and underscore _backstabAbility
Method arguments camelCase with an initial lowercase letter halfOrcArmy
Local variables camelCase with an initial lowercase letter isHumanoid, levelCount
'Constants' Uppercase with underscores CLERIC_PLAYER, GAME_MASTER
Ennumeration keys Uppercase with underscores characterClass.MAGIC_USER, armorTypes.PLATE_MAIL

Notes

  1. Variable/method names in all lowercase with underscores SHOULD NOT be used unless mimicking another API.
  2. Acronyms in variable/method names SHOULD NOT be upppercased.
  3. Variable/method names SHOULD be written in English.
  4. Variable/method names SHOULD NOT be abbreviated to the point of being unclear.

Variables

  1. Variables SHOULD be initialized where they are declared, if possible in a way that indicates what type of value they will hold. Null initializations are acceptable.
  2. Variable declarations SHOULD NOT include extra spaces before the equals sign to align the variable values.
  3. Variable names SHOULD NOT include 'temp' or 'tmp'. -- all local variables are by definition temporary.
  4. Magic numbers SHOULD NOT be used. Use a constant instead.

Coding Style

Overview

  1. Function declaration:
    function checkForTraps(dexterity, level) {
    // Do stuff to check for traps here
    }
    var checkForSecretDoors = function (race, level) {
      // Stuff for check here
    };
  2. If statements:
    if (gotInitiative) {
      attackDragon();
    }
    else if (speaksDragon) {
      tryNegotiating();
    }
    else {
      runAway();
    }
  3. For statements:
    for (var i = 0; i < guards.length; i++) {
      rollTwentySided(guards[i]);
    }
  4. While statements:
    while (charactersInjured) {
      castCureLightWounds();
      charactersInjured = checkCharacterHealth();
    }
  5. Switch statements:
    switch (characterClass) {
      case 'ranger':
        // Ranger special stuff here
        // Fallthrough
      case 'fighter':
        // Do fighter stuff
        break;
      case 'magicUser':
        // Do mage-specific stuff
        break;
      default:
        // do nothing
    }
  6. Try-catch-finally statements:
    try {
      pickPocket();
    }
    catch (e) {
      lookInconspicuous();
      reportBack(e);
    }
    finally {
        runLikeHell();
    }
  7. Object literal:
    var obj = {
      spellName: 'Invisible Stalker',
      numberOfFighters: 3,
      checkForTraps = function() {
        // Do trap checking
      } 
    };
         
    var obj = { staff: 'Staff of the Magi', wand: 
      'Wand of Negation', misc: 'Boots of Elvenkind' };
        

Notes

  1. Function literals MUST include a space between the word 'function' and the parentheses. (Otherwise it appears to be a function with the name of 'function.')
  2. Line continuations should be indicated by double indentation.
  3. If-else statements (also while, et al) MAY be written on a single line, but MUST use brackets.
  4. Parenthesis in conditional statements (if, while, for, etc.) SHOULD have a space before them.
  5. Parenthesis in function declarations SHOULD NOT have a space before them.
  6. Commas SHOULD be followed by spaces.
  7. The colon in object literal notation SHOULD have no space in front of it, and be followed by a single space.
  8. Operators SHOULD both have a space before and after.
  9. Lengthy DOM element IDs or other string parameters SHOULD be placed into variables before using.