Live data from Hacker News

I don't chain everything in JavaScript anymore

allthingssmitty.com

21–30 of 54 posts

Re: I don't chain everything in JavaScript anymore

#21
post #9

I read two paragraphs of this and was already sure this article is AI generated. Read more, and "This isn’t just about..." there we go

Yeah, and the content is not really thought provoking by any means.

I was surprised to see such obviously low quality slop here on HN.

Re: I don't chain everything in JavaScript anymore

#22
Kinda feels like a code smell having to optimize your code this way. Normally, data state would be abstracted away in a data store (e.g., Redux), and then specific sub-selections are pulled out using pure functions, e.g., `getActiveUserNames`, `getActiveUsers`, `getTop5ActiveUsers`.

Re: I don't chain everything in JavaScript anymore

#23
Note that for little while now lazy iterators have been available:

- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

- Array.prototype.values https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

This addresses a few criticisms here, and the main criticisms I had.

Re: I don't chain everything in JavaScript anymore

#24
> 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

Re: I don't chain everything in JavaScript anymore

#25

The point of chaining is generally to avoid making intermediate objects.

the intermediate objects are still created, they just aren't given names/saved to a specific memory location somewhere do you mean like streaming APIs where intermediate data structures aren't created? Is that what javascript does w/map() etc?

> the intermediate objects are still created

Yeah; this is what the new iterator methods were intended to solve

https://x.com/MozDevNet/status/2029527411424219254

Re: I don't chain everything in JavaScript anymore

#27
> Yeah, it’s more lines. But each step is just sitting there. No decoding required.

I actually think the first code block is easier to read. It's a familiar (to me) and simple pattern that is quick to read. I don't get how it would require more "decoding" than the second example which is more disjointed and needs more "parsing" for such a trivial case. Maybe it's about what you're used to?

I agree there are downsides to chaining. With more complex operations it can complicate debugging, and readability can suffer, so chaining is not a good fit there.

Re: I don't chain everything in JavaScript anymore

#28

> 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…

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.

Re: I don't chain everything in JavaScript anymore

#29
post #25

Earlier quoted context omitted.

the intermediate objects are still created, they just aren't given names/saved to a specific memory location somewhere do you mean like streaming APIs where intermediate data structures aren't created? Is that what javascript does w/map() etc?

> the intermediate objects are still created Yeah; this is what the new iterator methods were intended to solve https://x.com/MozDevNet/status/2029527411424219254

Basically?

  // This type of usage creates intermediates
  array.map().filter()
  // But these would not?
  array.values().map().filter().toArray()
  array.values().reduce()

Re: I don't chain everything in JavaScript anymore

#30
post #25

Earlier quoted context omitted.

the intermediate objects are still created, they just aren't given names/saved to a specific memory location somewhere do you mean like streaming APIs where intermediate data structures aren't created? Is that what javascript does w/map() etc?

> the intermediate objects are still created Yeah; this is what the new iterator methods were intended to solve https://x.com/MozDevNet/status/2029527411424219254

unfortunately iterators have their own set of overheads and it's not clear that these new methods are any faster for most use cases.
Post reply on HN