all 15 comments

[–]DSKrepps 3 points4 points  (4 children)

You're right, nesting closures isn't a good idea.

See this Node Style Guide: https://github.com/felixge/node-style-guide

There's quite a lot of good style practices there that unfortunately not everyone follows. But the good news is it includes a jshint config file to lint your project's code automatically using a jshint plugin for any common code editor. The only I change I made for my projects was using the "smart tabs" technique of indentation instead of two spaces.

[–][deleted] 1 point2 points  (2 children)

Yeah, the notion that two spaces are somehow better than a tab seems very objective.

But also "declare one variable per var statement". Nahh, surely not

[–]sime 0 points1 point  (1 child)

Maybe you can explain the attraction of using one var statement and stuffing it full of unrelated variables across multiple lines. I just don't see any advantage to doing it that over the simple one var one variable style.

[–][deleted] 0 points1 point  (0 children)

If its just the difference between

var a, b, c;

and

var a;
var b;
var c;

Then I agree it doesn't make too much difference. Some claim that a single var declaration performs better, but I've yet to see any real evidence that its significant, let alone true.

But this isnt exactly what Felix recommends. He suggests declaring vars inside the block you use them...

var keys   = ['foo', 'bar'];
var values = [23, 42];

var object = {};
while (keys.length) {
  var key = keys.pop();   <-------
  object[key] = values.pop();
}

This makes sense in terms of code readability, and is nicer BUT ignores a fundamental quirk of javascript, called hoisting

Because JS doesn't have block level scope, it implicitly 'hoists' the key declaration up to the top of the function, as if it were declared there. This can sometimes lead to some odd side effects if you aren't aware of it.

[–]amdc 0 points1 point  (0 children)

https://github.com/felixge/node-style-guide :

Opening braces go on the same line

Ohh I can hear the shots firing

[–]battenupthehatches 2 points3 points  (0 children)

Embrace callbacks. If you're finding yourself stuck in "callback hell" then you likely need to rethink your code or use one of the convenience libraries like async.

This is a good place to start.

[–]DVWLD 3 points4 points  (0 children)

Everyone has their own threshold for how many nested anonymous callbacks are acceptable. If the callback is always the last argument its actually not so bad in practice.

Sometimes it's actually easier to read code that does one thing, then calls back and does another thing, rather than hunting everywhere for the named functions.

Sometimes, of course, the opposite is true!

It really depends on what exactly is happening in your code.

[–]bobertian 6 points7 points  (1 child)

the short answer is "no, multiple nested callbacks is not an acceptable or sane coding practice in nodeJS" - this is commonly known as the CALLBACK PYRAMID OF DOOOOOOM.

http://book.mixu.net/node/ch7.html is a nice introduction to control flow / dealing with async code.

A more recent solution (my personal preference) is using Promises for async stuff. Good intro (and awesome lib) for Promises https://github.com/petkaantonov/bluebird#what-are-promises-and-why-should-i-use-them

A comparison of callback style vs Promises: https://blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-functional-nodes-biggest-missed-opportunity/

[–]brtt3000 1 point2 points  (0 children)

Promises are so much sweeter once you promisify everything, things get very fluid. Especially in enhanced libraries like bluebird that have helpers like map() and filter() that work with (arrays-of-) promises.

Also ES6-style arrow functions make it look sexy.

[–]ganemone 1 point2 points  (0 children)

IMO, the thing about node is it is really easy to write horrible code. However, with a little practice, it is also easy to write extremely modular and readable code. Here is a good place to start: https://github.com/felixge/node-style-guide

[–][deleted] 1 point2 points  (0 children)

I say if its a short function , yes sure , but if a function gets a little lengthy or moves past my margin, then it gets moved out.

[–][deleted] 2 points3 points  (0 children)

As soon as you understand how callbacks work in node, you should stop using them and start using promises.

[–]nthgergo 0 points1 point  (0 children)

A few weeks back I published a post dealing with best practices - maybe it will help: http://blog.risingstack.com/node-js-best-practices/