Performance Zone is brought to you in partnership with:

Eric is the Editorial Manager at DZone, Inc. Feel free to contact him at egenesky@dzone.com Eric has posted 804 posts at DZone. You can read more from them at their website. View Full User Profile

Node.js Makes for Cluttered Code

01.26.2012
| 2863 views |
  • submit to reddit
According to a recent post by Peter Moberg on his blog Source Code Bean, your node.js code is being polluted by it's own callbacks.  Moberg's recent hacking in Node.js/Express.js has led him to appreciate its speed, but hate it's verbosity.  He's not the only Node.js user to experience cluttered code.  His post has already attracted 18 comments from his readers, many of who agree with his opinion, as well as suggesting some ways to stem the tide of callbacks, such as this one by Ekin Koc:

This issue goes out of control sometimes, leading to dozens of nested functions and stuff.  CoffeeScript’s syntax makes it a little bit easier to deal with it but it’s still no solution. I think it’s too late for JavaScript for this but maybe, CoffeeScript or some other dialect might provide c# 5 style async handling. --Ekin Koc

Sounds like CoffeeScript addresses, but does not get rid of, the problem of cluttered code in node.js.  Some commenters seem to think that part of the solution here should be found in how the programmer approaches flow control.  According to commenter Shankar, Moberg was trying "to use sequential flow control on [an] event-driven platform," and this could be fixed through flow control methods posted on github (LINK).  This may, like CoffeeScript, address some of the issues of polluted code, but Moberg suggests that the problem here may not be as much with node.js as it is with the nature of JavaScript itself:

I am just saying that event driven programming in JavaScript could be made much easier. --Peter Moberg

Comment with some further suggeststions for cleaning up cluttered code, or let us know of your own experiences with node.js below.
Published at DZone with permission of its author, Eric Genesky.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: