Skip to content

Instantly share code, notes, and snippets.

@vicapow
Last active October 6, 2015 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicapow/3012327 to your computer and use it in GitHub Desktop.
Save vicapow/3012327 to your computer and use it in GitHub Desktop.
Node.JS Style guide
// My first attempt at a style guide:
// 2 character spaces (no tabs!)
// ' > "
// no semis! (yeah, i said it.)
// operators:
// (where foo and bar have been declared previously)
foo = 2
bar = 'some string' + ' some other string'
// take away
// + space before AND after the operator
// object literals:
var a = {
foo : 'bar'
, bar : 'foo'
}
var b = { foo : 'bar' }
// take away
// + commas first
// + space before AND after colon
// + if an object only contains a single attribute, put it on one line.
// same for nested object literals
var a = {
foo : 'bar'
, foo2 : { foo : 'bar' }
}
// groups of variable declarations
var foo = 'bar'
, hello = 'world'
, wow = 'bob'
// take away
// + commas first
// + space before AND after equals sign
// + only one `var`
// function declarations:
function foo(arg1, arg2){
// ...
}
// take away
// + no space between "function", "()", and "{"
// + spaces between arguments
// + none of that "var foo = function(){...}" nonsense
// + no trailing semicolon
// if / else if / else statements:
if (something) doSomething()
else if(sometihngElse) doSomethingElse()
else{
doSomthing()
thatTakesToLines()
}
// take away
// + try to never use braces unless you have to
// + don't separate if-else statements with extra spaces
// + try to keep the entire statement on a single line
// + include semicolons at the end
// calling back callbacks:
function foo(cb){
if(something) return cb()
else somethingElse(function(){
return cb()
})
}
// take away
// + name your callback "cb" when ever possible expect when it doesnt make
// sense in particular contexts, ie., when using express middleware, use
// "next" or if you're using mocha, use "done"
// scope name collision:
function foo(someArg){
someFunction(function(someArg_){
someArg.doSomething()
someArg_.doSometing()
})
}
// take away
// + if you feel compelled to name an inner argument the same name of an outer
// argument, give the inner argument an extra "_" if you want to be able to
// continue to reference the outer argument. When reading the argument name
// aloud, call it "someArg prime"
// callbacks that take a large number of arguments and/or have large argument
// names that would push the callback past the 80 character limit:
someFunction(that, has, way, too, many, arguments, than, it, really, needs
, function(and, also, takes, a, callback){
// ...
})
// take away
// + put the function onto a new line with indentation but with a space before
// the function
// + again, no spaces in arguments to function delerations
// long string literals:
throw new Error("Some really long, yet still someone confusing error message "
+ "that really doesn't help you at all in finding your bug. but at least it"
+ "does gives you an error message.")
// take away
// + always respect the 80 column limit.
// + use double quotes instead of single quotes.
// + use the '+' symbol to allow the string to extend onto a new line.
// + always put the '+' symbol at the beginning of a new line one indent
// from the beginning of the original statement.
// comments:
// Some interesting and bref information about how the following function works
// and what it does
somethingComplicatedAndHardToUnderstand()
// take away
// + always put comments directly above the code they're in reference to.
// + put a space infront of the `//` and the text that follows it
// + if the comment goes beyond column 80, force the word wrap in the comment.
// multiline comments:
// reserve multiline comments for functions.
/**
* this is some function that does something
*/
function someFunction(){
// ...
}
// Required Semicolons
something()
;(function(){
// ...
})()
// "nice" comments:
// someones it's helpful to point out you're doing something that might look
// unindented when it really was.
// NOTE: assignment
if(something = wow()){
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment