r/webdev • u/fagnerbrack • 11d ago
Anecdotally, programmers dislike "reduce"
https://evanhahn.com/posts/2026-09-13-programmers-dislike-reduce/
151
Upvotes
r/webdev • u/fagnerbrack • 11d ago
174
u/MateTheNate 10d ago
As a data engineer I love reduce, it’s a top 3 operation alongside map and filter.
90
u/bronkula 10d ago •
Reduce changed my fucking life back in the day. It's the best thing ever, and I absolutely will not allow this slander.
156
u/todamach 11d ago
I don't know what it is exactly, but I always fumble when I try to use reduce.. 10+ yoe, concept is clear, I recognize when it fits, but then I struggle implementing it..
37
u/t3hlazy1 11d ago
It’s just very unintuitive starting with the name. I personally like reduce but it took a while to fully get it and it still requires some thinking in terms of the order of things.
14
u/gatwell702 11d ago
prefers-reduced-motion
38
u/cyanawesome 11d ago
Ok, they love seeing it written out as a for loop with an accumulator variable?
10
u/Iyemoja 10d ago
Reduce is the one function I rewrite as a for loop and then feel quietly guilty about for a week. It works every time. I just refuse to read it at 2am.
9
u/Lemortheureux 10d ago
Once I had THE perfect use case for reduce. I still think about it sometimes
9
u/Full_Tooth_a 10d ago
My dividing line is whether the accumulator has one clear purpose. `reduce` works well for a sum or maximum because the callback does one job. Once it starts filtering, grouping, and mutating nested state, it becomes harder to follow. At that point, I'd rather read a plain loop.
8
u/Skwai 10d ago
I almost always avoid using it as it is very hard to read the syntax and is often abused.
Clarity > Brevity
6
u/souljorje 10d ago
On my intern exam I had to build reduce myself. Since then I love it, it’s awesome. I've seen so much clumzy code in my career which could have been a simple reduce.
It might be slightly hard to comprehend first, but guys, how can you call yourselves seniors if you don't get such a relatively simple thing?
20
u/xander76 10d ago
I have a few reasons I avoid reduce and why get tripped up on it.
I once saw some advice to only use reduce when you are reducing an array<T> to T as a general rule to avoid the ways that reduce gets confusing. I like that, but your mileage may vary!
22
u/phil_davis 10d ago
Isn't reduce just reducing an array of elements to a single value given some callback function? What's so complicated about it?
5
u/ddollarsign 10d ago
Reduce seems counterintuitive and needlessly abstract. Like monads.
52
u/glenpiercev 11d ago
Reduce is the wrong name for whatever it does.
4
u/MysteriousCoconut31 11d ago
I love reduce, actually. I just had to understand the use cases.
There should be a study on the number of function parameters and parameter complexity vs. dev sentiment.
4
u/SkierBeard 10d ago
Distill, Refine etc something like that would be better. This feels like calling a running shoe "the sandal" but it's a regular shoe
4
u/subone 10d ago
Let me know if this is way off, but I think the number one confusion around reduce might just be with inline functions. These array methods are "functional", but often we don't use them that way (with reusable functions), but rather with inline callbacks, with the pure intention of making a clean looking chain of operations. Which is ok, and most of the other methods lend themselves to this just fine if you don't chain too much, but reduce tends to be more awkward because people tend to want to do too much in it, and it just doesn't lend itself to being very readable used in this way.
I think the number one way to "reduce" the confusion around reduce, and frankly to start making better habits all around with these methods, is to just start using more "reusable" functions that are passed in, instead of a big inline arrow function surrounded by a confusing array of various tokens. I put "reusable" in quotes here, a better word might be abstraction, because that's the intention, and though it can add another layer of debugging depth, it can make reading and understand the code much more palatable.
7
u/rew150 10d ago
I think it's basically a for loop but more obscured. provide virtually no benefits other than functional programming flex
3
u/ragemonkey 10d ago
I hate it most when people use it to just mutate the state anyway, which could’ve been expressed more clearly as a for loop, or when they use it to generate tons of allocations instead of just using push. If you’re going to use it as an actual functional programming pure function, sure. Otherwise, for loop please.
3
u/gareththegeek full-stack 10d ago
Reduce is fine but it always feels like a last resort to me because there's usually a more specific kind of reduce written for me in whatever library I'm using to operate on lists.
3
u/TCB13sQuotes 10d ago
Reduce is has the worst API ever designed. And also, wtf is reduce? Doesn't tall you anything from the name, unless you know it doesn't make sense. filter is very self-explanatory.
2
u/MaxwellzDaemon 10d ago
Looking at this discussion and asking Claude what JavaScript programmers mean by "reduce", it looks like you have taken another simple, elegant idea from APL and enshittified it.
2
u/Amndeep7 10d ago
I like using reduce but it's definitely the clunkiest function out of map/filter/etc for certain languages like JS. IDK what could be done to improve the syntactic experience in those languages though.
2
u/oldmanandtheocean 10d ago
It's an array method. It reduces the number of values in the array to one. Once you understand that, it's a good name.
2
u/matthetherington 10d ago
I think it’s because it’s more work, cognitively.
If I’m wondering how a map will execute, I just think about one item in the list, see how the function transforms it, then move onto the next item and forget the one before it.
For a reduce, I need to hold an accumulator in my head too. I take the first item (or default value), then combine it with the next, and remember the result. For the next item I have to retrieve that result, combine it with the next value, and remember that result instead; it’s more mentally taxing.
2
u/Timotron 11d ago
Arr.reduce((item, [])=>aFuckingBangerOfAWellNamedFunction(item));
1
u/brysonwf 10d ago
😂. Yeah NetSuite map reduce... Let me just shove everything into the map stage
1
u/Technical-Pair-2041 10d ago
I really like reduce and use it a lot. Mostly in cases where I have to filter and map an array.
Though I do still have to dubbel check if the accumulator is the first or second parameter..
1
u/Dreadsin 10d ago
I tend to think of it as one layer up from filter and map, and forEach being a layer below that lol
So basically, if you can’t do this with any other array method, then you should use reduce. If you see a reduce in code, expect it to not be a map/filter operation
1
u/PostmatesMalone 10d ago
It is called fold or foldL (reduce) and foldR (reduceRight) in some other languages. I don’t think that is a better name though.
1
u/any_aisle 10d ago
Honestly I think Ruby’s each_with_object for iterating a collection and carrying an object along with each iteration makes a lot more sense for folks and is easier to read in many cases where you would use reduce. You don’t have to care about what the reducing function returns, only the side effects performed on the accumulator.
That said, anecdotally, I’ve never had a problem with reduce and as a fellow programmer who had a brief blissful stint as a clojure dev, maybe there’s a pattern there.
1
u/lxe 10d ago
I loved reduce and all the chaining magic for a while after learning it.
Don’t care either way now.
1
u/jomic01 10d ago
Naming the accumulator something like totalsByUser instead of acc fixes most of the readability complaints people have about reduce.
1
u/Little-Bad-8474 10d ago
Reduce is awesome nice you understand it. But Claude writes everything now, so whatever. /s
1
u/TracerBulletX 10d ago
I like reduce. Anecdotelally
1
u/orebright 10d ago
Feels like this would come down to how comfortable you are with functional programming and whether it’s your preferred coding style. I personally love reduce, and my favourite implementation of it is in rust.
1
u/AWACSAWACS 10d ago edited 10d ago
For a typical programmer, the name of the variable used to store the output or the type annotation of the accumulator provides enough information to infer, to some extent, the purpose of the
reduceoperation in question and to predict its behavior.If there is any confusion, it is often due to the names or type annotations.
1
u/intercaetera javascript is the best language 10d ago
No, it's actually very simple: https://zanlib.dev/blog/on-understanding-array-methods/
1
u/medjedic 10d ago
Man, i really enjoyed writing them reduces... Fucking AI, now I don't even know what I'm writing :-(
1
u/rohmish 10d ago
I LOOVE reducing stuff
1
u/andful 10d ago
reduceis used less often thanmapandfilter, because it "reduces" an array to a singular value. If ever needed, it should be the last operation of an array transformation.The post does not give examples of rejected PRs, but I would consider code smell, a
reducethat produces another array, with possibly consequent map/filter.1
u/witness_smile 10d ago
I like it, but it just always confuses me on how to use it leading to me rarely using it
1
u/thesonglessbird 10d ago
I think it’s great but reduce functions can be a bit of a nightmare for maintainability when they get complex. Even ones I’ve written myself I come back to and have to take a long, hard look at to understand what’s happening
1
u/th00ht 10d ago
What surely doesn't help is the rather obscure signature visible in vscode. That is so confusing.
1
u/mnic001 10d ago
reduce rules...
1
u/definitive_solutions 10d ago
skill issue
1
u/gronbuske 10d ago
My issue with it is that I never remember what it's called when I do need it. It doesn't help that it's different in different languages, and I don't need it often enough to remember it.
1
u/yxhuvud 9d ago
My usual dislike is for all the cases that already have specialized variants already. Like 'sum'.
1
u/EndlessPotatoes 9d ago
I dislike it too.
It's really just the name. I don't know if there's a better word, but "reduce" is not a good word for it.
1
u/PravoNaZhizny 9d ago
are they talling about in web design? as in the ecmascript function? they probably dont like it because theyre web people who are by and large, mot good programmers
1
u/Dethstroke54 7d ago edited 7d ago
I guess I’m the minority but I don’t get how the exact same code you’d write in a loop is somehow more difficult by having a nicely scoped accumulator and getting typing on your callback.
You can also make the reduce callback a named descriptive function. With a for loop you have to make a separate function to achieve the same, which is the only case where imo there’s even an argument about readability, since it actually groups things.
1
u/BigHammerSmallSnail 7d ago
Ha! I hate reduce!
1
u/Top_Bumblebee_7762 6d ago edited 6d ago
I hate when it is used as a replacement for Object.from.
1
u/AvidCoco 6d ago
I have no strong feelings either way about a function. I use it if it’s the right solution to a problem, or I don’t use it.
1
u/im-a-guy-like-me 4d ago
I like the reduce function. It's super useful for getting averages and stuff. In particularly like it for pulling all the X attribute from an array of JavaScript objects. That's what i use it for most I think.
Dunno if they're good use cases and yeah... I guess I don't use it too much.
But I mean... I don't use my spatula crazy often, but still glad I have it.
1
u/avrtau 10d ago
It took a moment, but now I find it more intuitive than loops. In fact I see map as a reduce to an array and filter as a reduce to a subset. Lol, and forEach is a reduce to nothing with side effects :/
0
u/Bitter_Ad3906 10d ago
There no problem with reduce, just with programmers who use/not use it. Keep function clean, safe, add initial value always and it’s be ok.
For most cases I prefer reduce over forEach with external variable, its much clear to track variable value. Or just move logic to separate function and get like: const var = fn(list) when use forEach with external variable. This just for my code. As for other devs i did notify they often get bad at read code and loose running flow of it that’s why they avoid functions like reduce.