, 13 tweets, 4 min read
My Authors
Read all threads
All code using array.reduce should be rewritten without array.reduce so it's readable by humans *mutes thread*
Ok ok, fair enough, there are cases where array.reduce is the best thing to use.

Specifically, cases where you want to look really smart and don't care about readability.
I mean, wtf: Messy array.reduce code example from https://developers.google.com/web/fundamentals/primers/promises
wait …authored by me
(although, in my defence developers.google.com/web/fundamenta…)
Whoa, this is worse than that time I pissed off Trump supporters
Here's a great quote from the inventor of Python (h/t @tigt_):

"the applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly"

artima.com/weblogs/viewpo…
BUT, on the other hand, Jimmy-randbro tells me I'm too dumb to appreciate the majesty of reduce, so WHO KNOWS.
Ok, I've been asking folks for good examples of reduce, and they fall into a few categories. Here's what I've seen:

SUMS! Yes, this is the textbook example, and one of the few ok uses of reduce. The reduce example is probably better here. const arr = [1, 2, 3];<br />
<br />
// Reduce:<br />
arr.reduce((total, item) => total + item);<br />
<br />
// Not reduce:<br />
let total = 0;<br />
for (const item of arr) total += item;
Folks suggested other forms of 'gathering', such as concatenating the keys of an object.

In this case I think the map + join is more expressive. However, I'd go for the loop if the two iterations caused performance issues (which is unlikely). const arr = [<br />
  {foo: 'hello'},<br />
  {foo: 'world'},<br />
];<br />
// Should become
// Reduce:
arr.reduce((str, item) => str += item, '');

// Not reduce:
arr.map(item => item.foo).join('');

// Loop:
let str = '';
for (const item of arr) str += item.foo;" src="/images/1px.png" data-src="https://pbs.twimg.com/media/ENbkzIJXUAEsaxa.jpg">
Another interesting example was "getting the size of the largest string". Again, I find the map + max version better describes the intent.

"Is the length of the first item bigger than the biggest item so far? If so, remember it"

vs

"Get all the lengths, give me the biggest" const arr = ['one', 'two', 'three'];<br />
// Should be 5<br />
<br />
// Reduce:<br />
arr.reduce((largest, item) => Math.max(largest, item.length), 0);<br />
<br />
// Not reduce:<br />
Math.max(...arr.map(item => item.length));
Most suggestions were object transformations. I strongly believe these should not use reduce.

Most were creating the object once, then passing the same object back through reduce. This is using reduce for decoration only. The only part of reduce it's using is the looping. // Array to object keys<br />
const arr = ['hello', 'world'];<br />
// Should become {hello: null, world: null}<br />
<br />
// Reduce:<br />
const obj = arr.reduce((obj, item) => {<br />
  obj[item] = null;<br />
  return obj;<br />
}, {});<br />
<br />
// Map + fromEntries:<br />
const obj = Object.fromEntries(<br />
  arr.map(item => [item, null]),<br />
);<br />
<br />
// Loop:<br />
const obj = {};<br />
for (const item of arr) obj[item] = null;
I saw ones similar to the above, but the author had realised the reduce was being used for decoration. So to avoid that, they create a new object on each iteration.

This is not only obfuscated, it's O(n2) slow.

It might make you look smart to other developers though! 😀 const obj = arr.reduce((obj, item) => ({<br />
  ...obj,<br />
  [item]: null,<br />
}), {});
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Jake Archibald

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!