Skip to content

Latest commit

 

History

History
322 lines (257 loc) · 4.57 KB

File metadata and controls

322 lines (257 loc) · 4.57 KB

JavaScript

Spacing

Use Spaces (Soft Tab), Not Tabs (Hard Tab)

// Good
var aVar        = 123;
var otherVar    = 'another';

// Bad
var aVar		= 123; // Note hard tabs.
var otherVar	= 'another';

Align at 4-character

// Good
var aVar        = 123; // Note aligned at 16th column.
var otherVar    = 'another';

// Bad
var aVar      = 123; // Note aligned at 14th column.
var otherVar  = 'another';

Variables

One Variable Per var

// Good
var foo = 123;
var bar = 'another';

// Bad
var foo = 123,
    bar = 'another';

// Bad
var foo = 123
  , bar = 'another'
  ;

Align at the Equal Sign Using Tab-Stops

// Good
var aVariable       = 123;
var otherVariable   = 'another';

// Bad
var aVariable = 123;
var otherVariable = 'another';

// Bad
var aVariable     = 123;
var otherVariable = 'another';

// Bad
var aVariable =     123;
var otherVariable = 'another';

Operators

At Least One Space Either Side of Operator

// Good
foo = 'value';

// Bad
foo='value';

// Bad
foo= 'value';

// Bad
foo ='value';

Conditionals and Loops

Always Wrap Body in Braces

// Good
if( condition ){
    doSomething();
}

// Bad
if( condition )
    doSomething();

// Bad
if( condition ) doSomething();

Body Gets Own Lines

// Good
while( condition ){
    doSomething();
}

// Bad
while( condition ){ doSomething(); }

Open Brace On Keyword Line

// Good
for( condition ){
    doSomething();
}

// Bad
for( condition )
{
    doSomething();
}

Closing Brace Gets Own Line

// Good
if( condition ){
    doSomething();
}
else {
    doSomethingElse();
}

// Bad
if( condition ){
    doSomething();
} else {
    doSomethingElse();
}

// Bad
if( condition )
{
    doSomething();
} else
{
    doSomethingElse();
}

Parentheticals

At Least One Space Inside

// Good
if( someVar == true ){
    // Do something.
}
aFunc( param );

// Bad
if(someVar == true){
    // Do something.
}
aFunc(param);

No Space Outside Next To Keywords

Object Literals

Opening Brace On Line With Variable or Function

// Good
var foo = {
    field1 : 123,
    field2 : 'another'
};
someFunction({
    field1 : 123,
    field2 : 'another'
});

// Okay
var foo = { field1 : 123 }; // Note only 1 field in object.
someFunction( { field : 123 } );

// Bad
var foo = { field1 : 123, field2 : 'another' };
someFunction( { field1 : 123, field2 : 'another' } );

// Bad
var foo =
    {
        field1 : 123,
        field2 : 'another'
    };
someFunction(
    {
        field1 : 123,
        field2 : 'another'
    }
);

Align on the Colon Using Tab-Stops

// Good
var obj = {
    aField          : 123,
    anotherField    : 'another'
};

// Bad
var obj = {
    aField : 123,
    anotherField : 'another'
};

// Bad
var obj = {
    aField       : 123,
    anotherField : 'another'
};

// Bad
var obj = {
    aField :        123,
    anotherField :  'another'
};

Functions

Align Consecutive Calls

// Good
var foo         = require( 'foo'        );
var other       = require( 'other'      );
var something   = require( 'something'  );

// Good
var foo         = fooFunc(      'foo'       );
var other       = otherFunc(    'other'     );
var something   = someFunc(     'something' );

// Okay
var foo         = require( 'foo' );
var other       = require( 'other' );
var something   = require( 'something' );

// Bad
var foo         = fooFunc( 'foo' );
var other       = otherFunc( 'other' );
var something   = someFunc( 'something' );

Separate Definitions With A Horizontal Line

// Good
function foo(){
    doSomeStuff();
}

// ---------------------------------------------------------------------------------------------- //

function bar(){
    doOtherStuff();
}

// Good
var AClass = (function(){
    function AClass(){
    }
    
    // ------------------------------------------------------------------------------------------ //

    AClass.prototype.aMethod = function(){
        doStuff();
    };
    
    // ------------------------------------------------------------------------------------------ //
    
    return AClass;
})();

// Bad
function foo(){
    doSomeStuff();
}

function bar(){
    doOtherStuff();
}

Classes

Documentation and Comments

Use JSDoc-Style Documentation

Use C++-Style Comments

Put a Comment Every 4-8 Lines

Miscellany

Always End Lines With Semicolon

Use CamelCase Naming