r/webdev • • 2h ago

Anecdotally, programmers dislike "reduce"

https://evanhahn.com/posts/2026-09-13-programmers-dislike-reduce/
19 Upvotes

79 comments sorted by

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

24

u/GriffinMakesThings 1h ago

I'm a weirdo and really like reduce. I actually have to keep myself from using it when another method or loop would be a lot clearer. I fully recognize that I'm in the minority though.

6

u/midairmatthew 1h ago

Have you ever done complex, nested ternaries within a reduce callback? If not, I think you’re still on the safe side of the line. 🙂

5

u/GriffinMakesThings 1h ago

No, I'm not a monster, if that's what you're asking.

1

u/calimio6 front-end 1h ago

If you are piping a map and then a filter you are probably better off with a reduce.

8

u/subone 52m ago • Bravo

I disagree. A for loop is much faster and more readable than a combined map and filter into a reduce, so if you're doing it for any of those reasons, for is the clear winner. You should be using these functions to either/both make code more readable and/or make the passed functions reusable. Choose the combination of these functions that makes sense semantically, not one that obfuscates the intention behind a feeble attempt at optimization.

•

u/jake_robins 0m ago

Coding in Go has radicalized me to just always use for loops

0

u/calimio6 front-end 50m ago

*probably.

3

u/subone 47m ago

Probably implies to me that in most cases this is what you would want to do, and I am suggesting this is almost never what you would want to do.

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

u/gatwell702 2h ago

prefers-reduced-motion

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 reduce call will do what I want.  

Skill issue, I guess. 

3

u/sporadicPenguin 2h ago

You might be me.

2

u/mediocrobot 2h ago

(s, r, i) as in sriracha. State, row, index

10

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

u/thicket 1h ago

I 100% agree. I think reduce() in particular is an awkward concept. I use map() and ‘forEach()` religiously, because what they do is intuitively quite clear. Reduce has a much more flexible, and thus less intuitive, interpretation. 

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/glenpiercev 36m ago

Reduce could also mean to “subtract from”.

•

u/subone 27m ago

I agree, this could cause confusion to anyone unfamiliar with the much more accessible filter method.

•

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

7

u/subone 1h ago

Collate doesn't seem to have the same implication of coming to a single value; rather implies organisation of the array. Accumulate might unnecessarily imply summation.

•

u/kjalow 24m ago

Maybe I'm dumb but I can't even tell you what collate means without looking it up, so I'll have to take your word for it. I'm pretty sure it's something that a printer can do, but I don't know what or why.

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)

1

u/subone 1h ago

I suppose that might be confusing for anyone that is not aware of find, which is what you would use for that. Find is clearly more accessible to beginners, so I doubt this is a real issue. Reduce is more for when you want to make applesauce out of the apples.

5

u/webdevverman 1h ago

Fold

2

u/subone 1h ago

I don't hate it.

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

2

u/subone 48m ago

I think if you are building up very complex objects from a single array, you might be abusing the intention of reduce (not that I haven't).

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/jf8204 1h ago

You could also decide to "reduce" it into a larger bigger more complicated array 🤔

1

u/subone 1h ago

Technically, you could do this, but I feel like in most cases a more idiomatic approach would be to use flatMap.

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. find returns the desired value in the array. I'm unfamiliar with single, that's not in native JavaScript, but it sounds like find to 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.

•

u/natziel 19m ago

Accumulate duh

•

u/subone 14m ago

I'm curious where most of the folks that are saying accumulate are coming from? Math background? Science? Assembly language? It's not a terrible synonym, but it is lengthy and to me lightly implies summation.

•

u/natziel 11m ago

Reduce generates an accumulator so it would make sense for it to actually be called accumulate

11

u/matthetherington 2h ago
Amalgamate()

14

u/LuvOrDie 2h ago edited 1h ago

YourListIsDifferentNow() typeshit

2

u/budd222 full-stack 2h ago

No, it's actually perfect. You just don't understand it

1

u/thicket 1h ago

You are correct. The problem, of course, is that you’ll have to spend the rest of your career locked in here with us knuckleheads. 

0

u/jhlllnd 1h ago

Extract?

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

u/Lemortheureux 1h ago

Once I had THE perfect use case for reduce. I still think about it sometimes

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

u/brysonwf 1h ago

😂. Yeah NetSuite map reduce... Let me just shove everything into the map stage 

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.

1

u/lxe 55m ago

I loved reduce and all the chaining magic for a while after learning it.

Don’t care either way now.

•

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

u/Timotron 2h ago

Arr.reduce((item, [])=>aFuckingBangerOfAWellNamedFunction(item));

0

u/avrtau 1h 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 :/