Hacker News new | ask | show | jobs
by erikpukinskis 3748 days ago
I have yet to see an "improvement" on callbacks, whether it's promises or fibers or generators, where the benefit in readability is worth the havoc it wreaks on my ability to debug the program.

These days I write JavaScript using only functions, literals, variables, and the occasional prototype and it's amazing.

I think many programmers have a desire to believe they are working on complex problems that demand sophisticated tools. I remember learning Ruby and being excited whenever I found a reason to write a DSL. In retrospect it was unnecessary every time. In every case the code would've been clearer if I had just stuck with functions and kept refactoring until I had the right interfaces and data structures.

It helps to remember

    function a() {
      b(function() {
        //etc
      })
    }
is equivalent to

    function a() {
      b(c)
    }

    function c() {
      //etc
    }
which is not particularly more verbose. And as a side benefit, refactoring that way gives you an opportunity to make c() self-documenting.
3 comments

> It helps to remember

> function a() { b(function() { //etc }) } is equivalent to

> function a() { b(c) } function c() { //etc } which is not particularly more verbose. And as a side benefit, refactoring that way gives you an opportunity to make c() self-documenting.

It's not always equivalent, since you can have closures.

That's true, and I use closures sometimes. But they are a performance and readability anti-pattern, and it's often better to either pass in or bind the data you actually need.

In some sense closures are globals and globals are bad.

Unfortunately, Node.js decided that every callback should accept an error as the first argument to every callback. If you're chaining a bunch of callbacks, it's tedious and error-prone to add boilerplate to check for an error and handle it consistently in every callback, and violating the DRY principle. It's not easy to simply propagate the error to a higher-level handler.
I find the `async` library excels at this:

  var fs = require('fs');
  var async = require('async');
  async.waterfall([
    (cb) => fs.readFile('foo.txt', cb),
    (data, cb) => my_function(data, cb),
    (result, cb) => myDb.lookup(result, cb),
  ], (err, finalResult) {
    if (err) {
      console.error(err);
    } else {
      console.log(finalResult);
    }
  });
There are all sorts of helpful async primitives in there. I don't write Javascript without it!

Here is a link to the documentation: https://github.com/caolan/async

I would have to look at your code, but generally if you have a long callback chain and you're trying to propagate errors up it you are doing something wrong. Are you really working on 10 nested activities simultaneously, or are you just nesting things because it's convenient and you don't want to think about how to do things in stages?

If you're trying to convince me that "callbacks become painful in situation X" you really don't need to. I know that. What I'm saying is that 9 times out of 10, the answer to the question "do we really want to be in situation X in the first place?" is "no". But instead of getting out of a bad situation by refactoring, people just write crazier and crazier control structures (i.e. promises) to make those bad situations workable.

Its a problem yes, but the nature of javascript makes this a difficult problem to address. Node tried addressing this with the 'domain' module to help with propagating errors upward more generically:

https://nodejs.org/api/domain.html

But it was deprecated: https://github.com/nodejs/node/issues/66

promises are a very nice way of drying out error handling and cleaning up callbacks for now.

If you're repeating yourself, then you could possibly abuse higher class functions to automatically generate error handling?
Yes, there are libs like errTo [1] and iced-error/make_esc [2]. Not to mention Promise.promisify(). Long solved problem.

[1]: https://www.npmjs.com/package/errto

[2]: https://www.npmjs.com/package/iced-error

> I have yet to see an "improvement" on callbacks, whether it's promises or fibers or generators, where the benefit in readability is worth the havoc it wreaks on my ability to debug the program.

Chrome has support for debugging async code - for example you can step from one code block to the code in callback as if it would be executed sequentially. So the 'debuggability' is a problem which could be solved on the side of the tools. Of course the case where there would be one single standard for handling async would vastly simplify situation for the tools developers.