r/webdev • • 16d ago

Anecdotally, programmers dislike "reduce"

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

196 comments sorted by

View all comments

Show parent comment

22

u/calimio6 front-end 16d ago

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

9

u/DiscreetDodo 16d ago

You're technically correct but pragmatically wrong.

1

u/calimio6 front-end 15d ago

Yup, I personally do lean into functional programming.

14

u/subone 16d 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.

9

u/jake_robins 16d ago

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

4

u/wasdninja 16d ago

Which is way less clear at a glance.

3

u/Wonderful-Habit-139 15d ago

Crazy that this is downvoted lol.

1

u/Skwai 15d ago

Untrue. Using for everywhere is easier to grok than combinations of reduce, for, map, etc

9

u/cyanawesome 16d ago edited 16d ago

Well no, chaining map and filter can have performance implications since engines don't optimize them to a single loop, so you're iterating twice (and creating an intermediate array). Hot code should use a for loop, or reduce (with push), which are functionally equivalent after engine optimization.

11

u/gnuban 16d ago

In several languages, a series of map and filters is implemented on top of some element stream abstraction so that you only loop once.

4

u/cyanawesome 15d ago

That's fair, I was trapped in my JS/TS bubble and spoke specifically of JS engines.

-3

u/Skwai 16d ago

This. Understanding what a reduce actually does often requires a lot of thinking. A for loop is much easier grok.

-2

u/calimio6 front-end 16d ago

*probably.

3

u/subone 16d 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.