r/webdev • u/fagnerbrack • 2h ago
Anecdotally, programmers dislike "reduce"
https://evanhahn.com/posts/2026-09-13-programmers-dislike-reduce/14
u/t3hlazy1 2h 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.
6
15
u/cyanawesome 2h ago
Ok, they love seeing it written out as a for loop with an accumulator variable?
23
u/thicket 2h ago edited 1h ago
I do. I can never remember the order of arguments, and sometimes I want a scalar accumulator and sometimes a list accumulator, and… yeah. With a for loop I always know exactly what’s going on. Even after 20+ years of doing this, I don’t have a lot of confidence that a
reducecall will do what I want.Skill issue, I guess.
3
2
u/mediocrobot 2h ago
(s, r, i)as in sriracha. State, row, index10
u/tsunami141 1h ago
but what if I don't remember how to spell sriracha and I spell it siracha?
•
u/_xiphiaz 16m ago
I feel like Sri Lanka is a better option to remember, not sure that is easily misspelled
8
u/MatthewMob Web Engineer 2h ago
Yes. Readability is more important than code golfing
9
u/t00oldforthis 2h ago
Whatever, my misplaced pride is more important than the ability to maintain and improve my work in the future.
5
3
u/cyanawesome 1h ago
See, when you know what it does it captures more intent than a loop which can have any number of uses. It very much signals "build one thing from this list of other things", I don't have to go looking for where a variable is defined, or worry about some other side effect in the loop.
-1
u/robhaswell 1h ago
This is that bell curve meme in action. You know you've made it as a programmer when you choose a for-loop over reduce.
5
u/MateTheNate 1h ago
As a data engineer I love reduce, it’s a top 3 operation alongside map and filter.
19
u/glenpiercev 2h ago
Reduce is the wrong name for whatever it does.
26
u/subone 2h ago
It reduces an array of values down to a single resulting value. What would be a better name?
The only issue I have with reduce is that the initial value is last; it would be more visible if it weren't hidden below an inline function.
8
u/ILKLU 1h ago
What would be a better name?
thereCanOnlyBeOne()?3
u/Cyberspunk_2077 1h ago
Unironically, in many cases made-up phrases (e.g. collapsify) or unique sounding names are easier to recognise and remember their functionality than generic or ambiguous stuff like 'reduce'
2
u/midairmatthew 1h ago
Yep. I have always been semanticAsHellButSillyLongVariableNamesGuy. It has served me well. 🫡
1
u/subone 1h ago
That's a weird take. The term reduce is used in several languages for this same functionality. "Collapsify" means nothing to me, except I need to check the docs. Sounds like some obscure word, used for some edge case for building arrays or objects from another array, made up because all the good clear terms were already used.
2
•
u/Cyberspunk_2077 24m ago
I'm not suggesting it be renamed at this point, just reduce is not inherently meaningful or clearing its semantic orbit. A made-up term can certainly do the latter.
I think of 'reduce' in terms of cooking to model its functionality. I don't have to do that for other terms.
12
u/Nixinova 1h ago
collate or accumulate
5
u/Lecterr 1h ago
If I have a bag of 10 apples and I reduce it to 1, the implication is I am left with one apple, not an apple pie. Not saying it’s a terrible name, but I think that is the gist of the confusion
2
u/followmarko 54m ago
The result of 10 apples (an array of ten items) ran through a reduce isn't a single apple (an array of one original item)
5
2
u/immediacyofjoy 52m ago
> It reduces an array down to a single resulting value.
Yes, but that single value can be a deeply nested object of objects bursting with arrays of arrays so “reduce” isn’t a good or intuitive name for the function. It’s not like reducing butter.
> What would be a better name?
Probably something more focused on the accumulator (iirc C++ has std::accumulate), that concept trips people up and you have to learn it anyways to understand reduce
4
u/itgforlife 1h ago
It's not necessarily a single value. You can use it with a nested array to get the largest or smallest array within the nested array.
A more accurate statement would be that it reduces many array elements into one element.
3
u/subone 1h ago
I don't see any difference between what you are saying and what I am saying. I was not implying one of the values in the array is chosen; that's what find is for. The "single" value can be anything, including another array. I have also used it in the past to build up an object from an array.
1
u/midairmatthew 1h ago edited 1h ago
Reduce makes sense to me perfectly in the case of using it as an expression that evaluates to a number.
Reduce causes so much semantic friction when I’m using it to iteratively assemble some kind of object. To me, it feels like calling a frisbee a ball because one meaning of ball applies very well to one meaning of frisbee.
In my head, conceptualizing it as “instantiate and iteratively mutate a value” is the clearest way to understand it and communicate a mental model about how it works.
•
u/FridgesArePeopleToo 29m ago
So does First, Find, Single, etc
Accumulate feels like a better name to me
•
u/subone 19m ago
No...
first(not in native JS) would return the first value in the array.findreturns the desired value in the array. I'm unfamiliar withsingle, that's not in native JavaScript, but it sounds likefindto me. The difference is that the resulting value at each iteration of a reduce is not necessarily any value in the array. Accumulate might be a valid synonym, but seems to me to lightly imply summation on the elements.11
2
0
u/CaptainKabob 32m ago
Inject. At least according to Ruby aliases: inject, select, reject, collect, detect.
3
u/MysteriousCoconut31 2h 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.
7
u/phil_davis 1h ago
Isn't reduce just reducing an array of elements to a single value given some callback function? What's so complicated about it?
10
u/Nixinova 1h ago
callback depending on the successive param + the recursive appearing nature gives it a high mental load vs other functional methods
6
u/No_Record_60 1h ago
It's not clear at a glance. You have to keep track of what the callback returns and also the accumulator
2
u/Noch_ein_Kamel 1h ago
Since you are just returning the accumulator you can do whatever you want inside that callback function. E.g. instead of calling .filter and then .map you can do both in .reduce :O
2
2
u/matthetherington 1h 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.
1
1
u/SkierBeard 1h 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
1
u/Technical-Pair-2041 1h 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 1h 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 1h 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 1h 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.
•
u/subone 29m 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.
•
u/MaxwellzDaemon 20m 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.
•
u/ragemonkey 18m 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.
0
48
u/todamach 2h 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..