I don't chain everything in JavaScript anymore
by AllThingsSmitty on 4/20/2026, 12:58:55 PM
https://allthingssmitty.com/2026/04/20/why-i-dont-chain-everything-in-javascript-anymore/
Comments
by: slooonz
One of the hardest problem in programming is naming your variables correctly.<p>I love chaining because it reduce the number of occurrences of that problem.
4/22/2026, 2:46:24 PM
by: another-dave
> Chaining nudges you toward “process everything,” even when that’s not what you meant to do.<p>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?<p>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().<p>As to:<p><pre><code> data .transform() .normalize() .validate() .save(); </code></pre> 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.<p>As apples-to-apples unchained equivalent isn't really any better<p><pre><code> const transformedData = transform(data); const normalisedData = normalise(transformedData); const validatedData = validate(normlisedData); save(validatedData); </code></pre> Is not more readable or understandable
4/22/2026, 1:18:07 PM
by: chuckadams
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.<p>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.
4/22/2026, 2:12:23 PM
by: __mharrison__
Folks need to learn how to debug chains. The juice of writing with intermediate variables is not worth the squeeze.
4/22/2026, 2:04:31 PM
by: latexr
The arguments for “process everything” don’t pass mustard, you’re not comparing the same thing.<p>> Chaining nudges you toward “process everything,” even when that’s not what you meant to do.<p><pre><code> const firstActiveUser = users .filter(user => user.active) .map(user => user.name)[0]; </code></pre> > This filters the entire array, maps the result, and then grabs one item.<p>> When what you actually wanted was:<p><pre><code> const user = users.find(user => user.active); const name = user?.name; </code></pre> Then if that’s what you wanted, do that!<p><pre><code> const name = users.find(user => user.active)?.name </code></pre> The fact that you processed everything in the first example was entirely your choice, it has nothing to do with chaining.
4/22/2026, 2:17:57 PM
by: t0mpr1c3
The point of chaining is generally to avoid making intermediate objects.
4/20/2026, 10:17:23 PM
by: kalaksi
> Yeah, it’s more lines. But each step is just sitting there. No decoding required.<p>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?<p>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.
4/22/2026, 1:30:04 PM
by: jrimbault
Note that for little while now lazy iterators have been available:<p>- <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...</a><p>- Array.prototype.values <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...</a><p>This addresses a few criticisms here, and the main criticisms <i>I</i> had.
4/22/2026, 1:17:58 PM
by: danfritz
I can only say: learn how to use reduce and you never loop twice through a list of items or objects.<p>To me reduce is very easy to reason about and makes it super easy to properly filter, combine, extract values without ending with filters on filters on maps and maps
4/22/2026, 1:52:43 PM
by: eugenekolo
Generally I agree with the sentiment, for one major reason: debugability/dev loops.<p>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.<p>I do think the example is somewhat loaded though, rename "result" to "top5ActiveUserNames" would do a lot there.
4/22/2026, 2:07:12 PM
by: po1nt
I prefer chaining. Less code usually means more readability. I don't want to save intermediate variable if I don't need it.<p>const filteredUsers = users.filter();<p>Why create a variable if we use it on a single place anyway. Feels redundant like prefixing "I" in interface names.
4/22/2026, 2:11:52 PM
by: ramon156
What a high-level boring list of examples. Why not have <i>one</i> complex example that clearly shows your "take". (Quotations, because these "I stopped doing X, and now doing Y" posts are stupid. They're also easier to write now with LLM's.)<p>I'm sure there's a case to be made for either, this post just doesn't do it.
4/22/2026, 1:14:09 PM
by: tantalor
This seems to have very little, if anything to do with JavaScript. These claims could apply to any programming language.
4/22/2026, 2:14:38 PM
by: prismatix
Not trying to sound snarky, but this is just part of transitioning from a junior/mid to a more senior developer: realizing that code readability matters more than terse-ness.
4/20/2026, 1:59:52 PM
by: rererereferred
There was a pipeline operator proposal for javascript using `|>`. Whatever happened to that?
4/22/2026, 2:00:58 PM
by: plumbees
I agree that debugging these pipelines are a nightmare sometimes. It's something that frustrates me sometimes because even though in OOP it won't be terse the action would be clearer. OOP can at times also introduce less cognitive load as well. I wonder if the issue is the mixing of paradigms. Although I don't think everything should follow purity boundaries: functional must always be functional and OOP languages should just be OOP but perhaps the mixture of doing functional programming in a OOP paradigm introduces unintended quirks that are cognitively taxing when bugs occur. (I've written 10 drafts and I'm not sure what I want to say so I'm going to just land it here and see what happens)
4/20/2026, 2:12:37 PM
by: takihito
When I see a magnificent series of functions, I feel a strange, indescribable feeling. However, it's difficult to feel the desire to actively engage with them.
4/22/2026, 1:11:03 PM
by: recursivedoubts
a non-AI similar take:<p><a href="https://grugbrain.dev/#grug-on-expression-complexity" rel="nofollow">https://grugbrain.dev/#grug-on-expression-complexity</a>
4/22/2026, 1:05:46 PM
by: l5870uoo9y
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`.
4/22/2026, 1:17:30 PM
by: mekoka
This is what tends to happen to code when your focus starts to shift away from how expediently you can write it and closer to how readable/maintainable it really is.
4/20/2026, 2:42:09 PM
by: joshstrange
Agreed, while chaining can look very pretty, it's a pain to re-parse and a pain to modify.<p>It's the same reason I don't like this style of function:<p><pre><code> .map(var => var.toUpperCase()) </code></pre> Sure, it's great today but but I want to debug it I need to add `{}` in and/or if I need to add a second operations I need to add the curly braces as well. That's I prefer explicit:<p><pre><code> .map((var) => { return var.toUpperCase(); }) </code></pre> Since it's much easier to drop in a debug line or similar without re-writing surrounding code. It also makes the git diff nicer in the future when you decided to do another operation within the `.map()` call.<p>I've asked many people to re-write perfectly functioning code for this same reason. "Yes, I know you can do it all in 1 line but let's create variables for each step so the code is self-documenting".
4/20/2026, 2:18:00 PM
by: efilife
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
4/20/2026, 7:16:39 PM
by: kubik369
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.<p>For me, the problems with chaining from the point of mostly maintaining existing software are:<p>1. Harder to impossible to reason about.<p>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.<p>2. Harder to debug<p>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.<p>3. Related problem - return chaining<p>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:<p><pre><code> foo() { return x .map() } baz() { return foo() .map() } fbaz() { return baz() .map() } </code></pre> 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<Map<String,String>,List<String>>. Do this instead even though it looks "less clean"/uses a supposedly useless variable:<p><pre><code> foo() { const helpfulName = x.map() return helpfulName } baz() { const anotherHelpfulName = foo.map() return anotherHelpfulName } fbaz() { const superHelpfulName = baz.map() return superHelpfulName } </code></pre> 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.
4/22/2026, 1:52:38 PM
by: khelavastr
Name return variables bro.<p>This is a solution that people fixed 25 years ago with detailedReturnObjectNames.
4/20/2026, 9:57:18 PM
by: bena
Uh huh.<p>I thought this would have some decent insight as to memory usage or something.<p>Nope. It's just clinically stupid.<p>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.<p>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.<p>Maybe we need fewerthingssmitty
4/22/2026, 1:46:58 PM
by: draw_down
[dead]
4/22/2026, 1:28:08 PM
by: tears-in-rain
[flagged]
4/20/2026, 1:23:15 PM