Created
April 14, 2014 17:02
-
-
Save madbence/10665519 to your computer and use it in GitHub Desktop.
My preferred JavaScript coding style :3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// multiple vars | |
var fs = require('fs'); | |
var net = require('net'); | |
// no whitespace after function name | |
// no whitespace after `(` and before `)`, like real text | |
// whitespace before block scope, open in the same line | |
// camelCase function names | |
function foo(bar, baz) { | |
// ident with 2 spaces | |
// no whitespace after control structures (like a function call) | |
if(bar) { | |
// no whitespace for member access | |
// semicolons | |
console.log(bar[baz]); | |
// whitespace after block scope | |
// whitespace around operators | |
} else if(1 > 2) { | |
// omit whitespace to emphasize operator precedence | |
console.log(3*4 + 5*6); | |
} else { | |
// single quotes | |
throw new Error('qux!'); | |
} | |
// omit brackets only if conditional is one-liner | |
if(baz) return 'woof'; | |
} | |
// CamelCase class names | |
function Awesome() { | |
// Object literals, one property per line | |
foo({ | |
bark: 8 | |
}) | |
} | |
async.waterfall([ | |
// named anonymous functions | |
// callback is cb | |
function qux(cb) { | |
// error is err | |
doSomething(function(err, data) { | |
cb(err, data); | |
}) | |
}, | |
function baz(cb) { | |
// align parameters when function call is long | |
complicatedThing('foo', | |
'bar', | |
'baz'); | |
// group parameters when needed | |
cp.exec([ | |
'prog', | |
'-o', 'bar', | |
'-m', 'baz', | |
'--qux', '13', 'foo']) | |
} | |
// I'm not sure about identation for the function below... | |
], function(err) { | |
console.log('done'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment