4 ms·
The response by Isaac filled me with rage as soon as I got to 'No one actually writes code like this'. The guy provides a real example from his application, des
by kevingadd 16y ago
The response by Isaac filled me with rage as soon as I got to 'No one actually writes code like this'. The guy provides a real example from his application, describes how alternative solutions stack up, and how he'd like to solve the problem, and gets called out for presenting a 'strawman'. Sigh, newsgroups...
I personally share the original author's perspective on this and was disappointed to see more sophisticated methods of writing concurrent code (futures, coroutines, etc) dropped from Node in favor of raw callbacks. In my experience, chaining raw callbacks is too easy to get wrong for it to be considered a reliable way of writing all your asynchronous logic, even if it is a great primitive to build a framework on top of. In particular, the effort involved in propagating exceptions all the way through a callback chain is rather significant, and making a mistake in a single function undermines the reliability of the entire chain (usually resulting in work silently being dropped on the floor). I wish more of an effort was made to address this problem.
- abp 16y agoYou have seen what the author posted later on in the discussion? https://github.com/sam-mccall/node-plate https://github.com/sam-mccall/node-plate
- DanielRibeiro 16y agoThere are soo many interesting ways to solve this: http://news.ycombinator.com/item?id=2101724 http://news.ycombinator.com/item?id=2101724
- Sidnicious 16y agoIs that the wrong link, or are you threadjacking?
- DanielRibeiro 16y agoWow, wished I saw it earlier (can't edit now). Bad paste. Correct link: http://www.infoq.com/articles/surviving-asynchronous-programming-in-javascript http://www.infoq.com/articles/surviving-asynchronous-program...
- gregschlom 16y agoLink unrelated
- liuliu 16y agoI like this syntax more: https://github.com/creationix/do https://github.com/creationix/do
- moe 16y agoI like neither. They're still riddled with boilerplate (anonymous functions) and are quite horrible to read and write (bracket nesting). I sometimes wish node.js (and js in general) would adopt co-routines. They'd seem to be the perfect middle-ground here; async code that looks almost like threaded-code, and no more callback-hell.
- sam-mccall 16y agoAnd later still: https://github.com/sam-mccall/node-ship https://github.com/sam-mccall/node-ship (supports a few more things than just method chaining)
- patrickaljord 16y ago> Sigh, newsgroups... What does this have to do with newsgroups? You can get this kind of response on any medium, no need to blame it on newsgroup.
- raggi 16y agoI wholeheartedly agree with you that the comment "No one actually writes code like this" was inaccurate and not useful for the discussion. I see people doing this exact same style of code in EventMachine in ruby, and have done for years. I am continually having to advise newcomers to the evented space that they need to write state machines inside objects to maintain state and process flow, and advising them to wrap callbacks into message sends to these process containers. There is nothing lacking in javascript to do this (create state and function containers), just as there is no lack of OO in ruby. Despite this, newcomers to evented programming always do the same thing. They revert back to a primitive style of programming, writing very long, very tab-deep functions. This is bad practice, you don't do it in an imperative synchronous environment (after some experience), and you shouldn't do it in an async environment. You do not need to use closures for state passing. You shouldn't use closures as general state containers. They're even worse than using stack frames, as they're even less easy to debug (at least walking stack frames is easy with a decent debugger). They're really useful for the minor things, but to encode a complex process, they're completely inappropriate for reasons already discussed (error handling is an ever so common one in the network space). I think this may be what the person was trying to get at with their comment, but they completely lacked substance, which was no use to the OP. Everyone seems to end up discussing some new async-useful algorithms or datastructure that will "save the day", but in reality, until you start encoding application logic processes into decoupled, testable containers, you're always going to fight some complexity. This really has nothing to do with async, the principles of encapsulation and coupling are absolutely well discussed and documented, and merely apply even more vehemently to the async space. The unfortunate fact is that many of the users of these libraries do not factor code in this manner, and as such this discussion comes up again and again. I honestly don't know of any good open source examples of complex async applications in either language (ruby or javascript) that are well structured. I can point you time and time and time again at libraries that are poorly constructed based on the "trivia" examples that "don't require" any structure and so just use a single object/scope and some closures to achieve a minimal use case. There are many possible reasons for this lack of good examples. The reason I haven't written one is because it still takes some care, mostly in architecture, and no one will pay me to write such open source. There probably are some good examples in C, based on libev, libevent, glibs loop, etc, etc. I'm not intimately familiar enough with any example code bases to be able to call them out off the top of my head.