Hacker News new | ask | show | jobs
by another-dave 55 days ago
> Chaining nudges you toward “process everything,” even when that’s not what you meant to do.

It feels pretty clear that the chains in that example (filter/map) are meant for operating on collections. And that if you're searching for a single item then chaining isn't the way to go?

Personally, if I knew I wanted only a single item I wouldn't feel more "nudged" towards appending a [0] on the end of a long chain rather than doing a refactor to the find().

As to:

    data
      .transform()
      .normalize()
      .validate()
      .save();
here the problem isn't that you've done method chaining, it's that you've named your functions with terse names that you're going to forget what they do later on e.g. a generic "normalise" vs a "toLowerCase()" or whatever.

As apples-to-apples unchained equivalent isn't really any better

    const transformedData = transform(data);
    const normalisedData = normalise(transformedData);
    const validatedData = validate(normlisedData);
    save(validatedData);
Is not more readable or understandable
2 comments

It's easier (at least for me), in a debugger, to hover over the intermediate variables in the bottom version and see what the result is.
One benefit of the more verbose lower example is, that when it fails somewhere you get a clear line where it failed so can focus already deeper. In chain, unless I am mistaken here (maybe mixing java and javascript so sorry if I am off), its just a general error somewhere in that chain.

Nothing earth-shattering, but who never had to debug something someone saw in production once and never again, then any additional useful info is weighted in gold.