US English should be used.
preferred
var authorized = true;not preferred
var authorised = true;Use 2 spaces per indentation level. No tabs.
preferred
function sumArgs() {
for (var i = 0, sum = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}not preferred
function sumArgs() {
for (var i = 0, sum = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}Use semicolons.
Do not place semicolons after any function unless it is a declaration (like var y = function(){};).
Don't omit the semicolon after return.
preferred
var x = 12;
var y = function() {};
for (var i = 0, l = 5; i < l; i++) {
// something
}
function handler() {
// something
return;
}not preferred
var x = 12
var y = function() {}
for (var i = 0, l = 5; i < l; i++) {
// something
};
function handler() {
// something
return
};
;All names should be limited to using alpha a-z and numeric 0-9 characters. All names should start with an alpha character. One exception is when dealing with an outside dependency, like SQL, where a column name may contain underscores.
Names should be camel cased. Any constructor should start with a capital letter. Any other type of variable should start with a lowercase character.
Constants do not exist in Node, since the idea of a frozen variable is loose. Global variables should be named like normal variables.
preferred
var defaultValue3 = 30;
function Parent() {
this.myValue1 = 10;
this.myValue2 = 20;
this.myValue3 = defaultValue3;
}
var child = new Parent();not preferred
var DEFAULTVALUE3 = 30;
function parent() {
this.MyValue1 = 10;
this.myvalue2 = 20;
this.my_value_3 = 30;
}
var Child = new Parent();If possible, do not define a function, rather than assigned a function to a variable.
preferred
function doSomething() {}not preferred
var doSomething = function() {}Objects should always be broken across lines.
preferred
var obj = {
key1: 'val1',
key2: 'val2',
key3: {
key4: 'val4',
key5: 'val5'
}
};not preferred
var obj = { key1: 'val1', key2: 'val2', key3: { key4: 'val4', key5: 'val5' } };When coding with conditionals, the left hand margin of the code should be the "golden" or "happy" path. That is, don't nest if statements if you don't need to. Multiple return statements are OK.
preferred
function someMethod(err, callback) {
if (err) {
return callback(err);
}
// more logic
callback();
}not preferred
function someMethod(err, callback) {
if (!err) {
// more logic
callback();
}
}not preferred
function someMethod(err, callback) {
if (err) {
return callback(err);
} else {
// more logic
callback();
}
}If you have tiers of callbacks, then use the async module, or something similar, to make it more readable.
not preferred
module.exports = function(callback) {
methodOne(function(err, resOne) {
if (err) {
return callback(err);
}
methodTwo(function(err, resTwo) {
if (err) {
return callback(err);
}
methodThree(function(err, resThree) {
callback(err);
});
});
});
};preferred
var async = require('async');
module.exports = function(callback) {
async.series([
methodOne,
methodTwo,
methodThree
], callback);
};Every statement (if, for, while, do, switch, try, catch, finally) should have a space before the following { bracket or ( parenthese.
if (x) {
// logic
}Simple arrays do not need spacing. But if there are references like the one below, then it should have spaces included.
var arr = [1, 2, 3];
var arr2 = [ anotherObj[key] ];Vertical white-space is needed for readability. Don't shy away from using space to make things readable.
All if statements should be encapsulated by {} brackets.
No if statements should be a single line.
preferred
if (x === y) {
a = 10;
} else if (x === z) {
a = 20;
} else {
a = 30;
}not preferred
if (x === y) a = 10;
else if (x === z) a = 20;
else a = 30;not preferred
if (x === y)
a = 10;
else if (x === z)
a = 20;
else
a = 30;Try catch statements should follow the same rules as an if else. See the above documentation.
No function calls should be made from within a try (since any error within would bubble up to the catch)
preferred
try {
regionId = user.payment.region.id; // will fail if any part of dot notation is undefined
passed = true; // will only be reached if no err
} catch(err) {
passed = false;
}
if (!passed) {
processUnavailableRegion(user);
} else {
processAvailableRegion(regionId);
}not preferred
try {
regionId = user.payment.region.id; // will fail if any part of dot notation is undefined
processAvailableRegion(regionId); // any error in this call will bubble to the catch
} catch(err) {
processUnavailableRegion(user);
}Do not use new Array() or new Object(). Instead, use [] and {}. This is actually more efficient, and saves a few bytes.
Do not use eval. It, in general, is not very safe.
Do not use with. It leads to some confusing situations with scope.