Skip to content

Instantly share code, notes, and snippets.

@ilyavf
Last active October 15, 2015 17:08
Show Gist options
  • Save ilyavf/e2ad4135d65637b32440 to your computer and use it in GitHub Desktop.
Save ilyavf/e2ad4135d65637b32440 to your computer and use it in GitHub Desktop.

Right Notes

Coding Conventions

Whitespace

  • Always spaces, never tabs
  • 2 spaces indents
  • No trailing whitespace at end-of-line
function getSum(arr){
  var sum = 0;
  for (var i = 0; i < arr.length; i++){
    sum += arr[i];
  }
  return sum;
}

Semicolon

  • Always end statements with ;
// Right
console.log('even when not required');

// Wrong
console.log('even when not required')

Variable declarations

  • Give descriptive names
  • Do not use similar names or synonyms for different variables unless following a convention
  • for iterators should use single character names
  • Use combination of plural for array and singular for each item in the array
  • Use camelCase, never underscores
  • Avoid using numbered variables (e.g. i1, i2, i3)

Scope

  • All scopes must be wrapped in {}
// Right
if (condition) {
    return;
}

// Wrong
if (condition) return;

if (condition)
    return;
  • Define self for passing this into nested functions
Example.prototype.method = function () {
    var self = this;
    
    call(123, function (err) {
        self.display(err);
    });
};

Style

String literals

  • Always ' never " in JavaScript
// Right
var string = 'text in single quotes';

// Wrong
var string = "text in single quotes";
  • Use " in HTML:
<div class="my-class"></div>
  • Combine ' and " in JavaScript with HTML templates correctly:
var html = '<div class="my-class">{{myValue}}</div>';
  • Newline after { except for inlined or empty objects
// Right
if (condition) {
    execute(value, { strict: true });
}

if (condition) {
    var options = {
        strict: true
    };
    execute(value, options);
}

var empty = {};

// Wrong
if (condition) { execute(value, { strict: true }); }

if (condition) {
    var options = { strict: true };
    execute(value, options);
}

var empty = {
};
  • No empty line before end of scope
// Right
if (condition) {
    if (otherCondition) {
        console.log('done');
    }
}

// Wrong
if (condition) {
    if (otherCondition) {
        console.log('done');

    }

}

Spaces

Use one and only one space (when required)

// Right
var value = calculate(1, 3);

// Wrong
var  value =  calculate(1,  3);
  • No space between function name and ( when invoking a function
// Right
var key = example();

// Wrong
var key = example ();
  • No space after ( or before )
// Right
execute('order', 34);

if (result === 'ok') {
    console.log('success');
}

// Wrong
execute( 'order', 34 );

if ( result === 'ok' ) {
    console.log( 'success' );
}
  • No space before object key :, always after object key :
// Right
var obj = {
    a: 1,
    b: 2,
    c: 3
};

// Wrong
var obj = {
    a : 1,
    b :2,
    c:3
};
  • Always space after reserved keywords (if, else, for, return, function, etc.)
// Right
for (var book in books) {
    if (books.hasOwnProperty(book)) {
        console.log(book.name);
    }
}

// Wrong
for(var book in books) {
    if(books.hasOwnProperty(book)) {
        console.log(book.name);
    }
}
  • No space after [ and before ] in inlined arrays
// Right
var numbers = [1, 2, 3];

// Wrong
var numbers = [ 1, 2, 3 ];
  • Always space before and after operators, unless following an indent or end-of-line
// Right
var a = 1 + 3;
var b = 'john' +
        ' ' +
        'doe';

// Wrong
var a=1+3;
var b='john'+
      ' '+
      'doe';

Commas

  • Never begin a line with , (always at the end of the previous line)
// Right
execute('some error message',
        12345,
        this);

// Wrong
execute('some error message'
        ,12345
        ,this);

Multi-line statements

  • Statements should only be broken into multiple lines to improve readability
  • Break statements if they are longer than 150 characters long
  • No empty lines in the middle of a single statement
  • Indent multi-line statements
  • Conditions should be indented to the first character of the condition in the first line
if (result &&
    result.status &&
    result.status.statusCode === 200) {

    console.log('success');
}
  • Variable should be indented to the first character of the value in the first line
var message = 'hello' +
              ' and welcome';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment