Live data from Hacker News

I don't chain everything in JavaScript anymore

allthingssmitty.com

41–50 of 54 posts

Re: I don't chain everything in JavaScript anymore

#41
I prefer chaining. Less code usually means more readability. I don't want to save intermediate variable if I don't need it.

const filteredUsers = users.filter();

Why create a variable if we use it on a single place anyway. Feels redundant like prefixing "I" in interface names.

Re: I don't chain everything in JavaScript anymore

#42
The whole point of composable things like chains is that it's trivial to split them out into intermediate variables if you like. Or into other functions -- which JS's syntax makes slightly more annoying, but you can still depend on the semantics not changing from being moved around.

So if you like intermediate variables, great. I like them too. I also like having the option of chaining where it's necessary or just more expressive. Writing composable APIs means everyone wins.

Re: I don't chain everything in JavaScript anymore

#44

There was a pipeline operator proposal for javascript using `|>`. Whatever happened to that?

Death by endless committee, stuck in stage 2 until the heat death of the universe. Even PHP managed to get a pipe operator in the language before JS.

Re: I don't chain everything in JavaScript anymore

#45
The arguments for “process everything” don’t pass mustard, you’re not comparing the same thing.

> Chaining nudges you toward “process everything,” even when that’s not what you meant to do.

  const firstActiveUser = users
    .filter(user => user.active)
    .map(user => user.name)[0];
> This filters the entire array, maps the result, and then grabs one item.

> When what you actually wanted was:

  const user = users.find(user => user.active);
  const name = user?.name;
Then if that’s what you wanted, do that!

  const name = users.find(user => user.active)?.name
The fact that you processed everything in the first example was entirely your choice, it has nothing to do with chaining.

Re: I don't chain everything in JavaScript anymore

#47
post #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 fina…

1. Yes, you hit the nail on the head. It basically requires people to be more cognisant of this. However, I think telling people to break stuff out into intermediary variables is much easier to argue for than whether the function is getting a bit too long.

2. Yes, easy filterings usually don't need to be broken down/named, but it really depends. At the very least, if the culture is to name intermediary values, you might accidentally get useful information from the variable names even if people weren't diligently writing explanatory why comments.

3. This isn't Kotlin related, it is just that if you do not have a language/codebase with branded types (or some type system property I don't know the name of), the type system might only infer the base primitives of the result, ending up with stuff like the type I mentioned.

Re: I don't chain everything in JavaScript anymore

#49
post #13

Earlier quoted context omitted.

Can you elaborate on that? I'm not following. What makes return variables special? I'd expect that much of the time, the return variable is closely linked to the name of the function. So closely linked that it risks being redundant.

Not JS but I like having explicit return var assignments because it's easier to break on and inspect. I don't know why tools don't make this easier.

Ah, yes, I've had that problem. With some debuggers I can place a breakpoint after the return, but it won't tell me the value on the stack to return. I can look up the stack and hope at some point I'll be able to get a handle on it (assignment to a variable, passed in as an argument, etc) but sometimes I can't.

I probably won't adopt this as a convention, just because most of the time when I have access to a debugger I'll also have access to the source code and handle it myself. But I'm going to keep it in mind, and perhaps get fed up at some point and decide to adopt the idea. Thanks.

Re: I don't chain everything in JavaScript anymore

#50
Ageeed. The chained style is Points-free or tacit (never heard anyone use that term in the wild) programming. https://en.wikipedia.org/wiki/Tacit_programming

My opion here but: generally not a fan. Explicitness is a virtue. That itself can go overboard too!

In the olden days the chained syntax was also worryingly low performance: ideally you want to iterate only as many times as you have to, doing as much work per pass as you can rather than spreading it out across passes. The example code also creates intermediate arrays, versus working on place: not always possible but over allocating was another "are you trying to make this page look jank" trigger. The chained syntax prevents you from dealing with either, albeit iteration helpers somewhat ameliorate this.

Perl was very fun to write because there are so many predefined variables that would automatically be written to and read from. It allowed for imperative-ish code that still was incredibly tacit. All the data flow was implicit. Fun to write, incredibly hard to visualize.

I do wish we saw IDEs that made data flow analysis more visible. Overlay the source code with the compiler's sea of nodes, show the us the graph of compute that actually constitutes a section. I think we could be augmenting ourselves to do much more with implicit styles, to get the visibility of our code "for free", and at an even more explicit visually clear way than using lots of intermediate variables. Oh to dream.

Post reply on HN