Live data from Hacker News

I don't chain everything in JavaScript anymore

allthingssmitty.com

31–40 of 54 posts

Re: I don't chain everything in JavaScript anymore

#31
Uh huh.

I thought this would have some decent insight as to memory usage or something.

Nope. It's just clinically stupid.

His first example is pretty much the same either way. I would say the "better" way is a little more involved to read. But it's nothing either way.

His second example makes unnecessary chains. He filters, then maps. When he could just use find and get the name like he does in the "steps" version.

Maybe we need fewerthingssmitty

Re: I don't chain everything in JavaScript anymore

#32

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

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.

Re: I don't chain everything in JavaScript anymore

#33
I whole-heartedly sympathise with the problem author is trying to describe. He does not introduce it very well, but if you read through the whole thing, you should be able to get the gist of it.

For me, the problems with chaining from the point of mostly maintaining existing software are:

1. Harder to impossible to reason about.

As the author alludes to, 1-2 chains are fine, but it starts getting impossible when you get into a territory where you have a longer chain which has a deeper call tree. This happens over time where you start with a smaller chain and people start lengthening it, adding helper functions which grow into large call trees, etc. This makes it so that you have sort of a blackbox pipeline that is, at the very least, annoying and time-consuming to inspect.

2. Harder to debug

Author tries to mention this but he seems to fail/stop short of pointing out what is wrong with the example he provides. For me, I work with Kotlin. In Kotlin, you cannot put a breakpoint in the middle of the chain! As far as I know, you can only put a breakpoint inside of the chained function calls and do step-into/step-over and such, but you cannot put a breakpoint in-between chain function calls. This means that debugger is basically useless if your codebase looks as described in my previous point. The solution is to write a bit more code at the start, naming each variable. This makes it much easier to debug the code/logic (because you can put a breakpoint on the specific variable/step you are interested in) and, more importantly, to understand, because you explain the steps with the variable names and optionally also with comments.

3. Related problem - return chaining

Another issue I have in codebases I inherited is what I would describe as return chaining. It is what happens when you have code which returns a function call and the called function does the same thing and so on and so on. Minimalistic example:

  foo() {
     return x
        .map()
  }


  baz() {
     return foo()
        .map()
  }

  fbaz() {
      return baz()
        .map()
  }

This way, there is usually no good place to inspect the values and it is hard to reason about what even is the return type/value. Yes, the type system can take it, but good luck figuring out what is Map,List>. Do this instead even though it looks "less clean"/uses a supposedly useless variable:

  foo() {
     const helpfulName = x.map()
     
     return helpfulName
  }


  baz() {
     const anotherHelpfulName = foo.map()
     
     return anotherHelpfulName
  }

  fbaz() {
      const superHelpfulName = baz.map()
      
      return superHelpfulName
  }
In summary: please, for the love of all that is holy, resist the urge to write function chains, always store meaningful intermediary values in named variables with "why" comments in relevant places and do so especially with return values.

Re: I don't chain everything in JavaScript anymore

#36

I whole-heartedly sympathise with the problem author is trying to describe. He does not introduce it very well, but if you read through the whole thing, you should be able to get the gist of it. For me, the problems with chaining from the point of mostly maintaining existing software are: 1. Harder to impossible to reason about. As the author alludes to, 1-2 chains are fine, but it starts getting impossible when you…

1. The pipeline is simple to split or cut entirely, though. No reason to grow it into a monstrosity, but many reasons to not do it. This problem sounds similar to growing a function too much.

2. I agree in general when talking about more complex operations. Simple transformation and filtering rarely needs intermediate variables for readability or debugging. And the naming of result variable already describes the final collection.

3. Never had to deal with this kind of code but I haven't used Kotlin.

Re: I don't chain everything in JavaScript anymore

#38
post #20

a non-AI similar take: https://grugbrain.dev/#grug-on-expression-complexity

Which part in OP article indicated AI? Probably is, but hard to tell exactly.

Staccato prose, subtitles like a sales pitch.

Mostly though, it's a combination of a thousand little things. There's no perfect bullet point list for 'this is AI', and if there was, AI would be able to hide it.

What humans are good at is seeing the uncanny valley in both images and prose. It's an old test [0] and we haven't managed to formalise it (or, as above, would even want to), but it's reliable for people with sufficient literacy.

[0] https://en.wikipedia.org/wiki/I_know_it_when_I_see_it

Re: I don't chain everything in JavaScript anymore

#39
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, nowadays I'm finding more and more of such things and not wanting to read the article further. Sometimes I feel we ignore some good pieces because of LLMification! When I write, I now use LLMs as an alternative to Grammarly and explicitly instruct it to not to rewrite. Sometimes the choice of words are very intentional and LLMs dont "feel"/understand the emotional reason behind that.

> Sometimes I feel we ignore some good pieces because of LLMification!

As a heuristic for attention, "does the claimed author care about, understand, or even know what they're writing about?" is not a bad start.

Re: I don't chain everything in JavaScript anymore

#40
Generally I agree with the sentiment, for one major reason: debugability/dev loops.

Inevitably you're going to end up having to debug that each of those steps is correct, for that you'll find it a lot easier to break it out.. and the next person who has to do it will as well.

I do think the example is somewhat loaded though, rename "result" to "top5ActiveUserNames" would do a lot there.

Post reply on HN