Reduced to a Union – The Daily WTF

Date:

Share:

The code Clemens M supported worked just fine for ages. And then one day, it broke. It didn’t break after a deployment, which implied some other sort of bug. So Clemens dug in, playing the game of “what specific data rows are breaking the UI, and why?”

One of the organizational elements of their system was the idea of “zones”. I don’t know the specifics of the application as a whole, but we can broadly describe it thus:

The application oversaw the making of widgets. Widgets could be assigned to one or more zones. A finished product requires a set of widgets. Thus, the finished product has a number of zones that’s the union of all of the zones of its component widgets.

Which someone decided to handle this way:

zones.reduce((accumulator, currentValue) => accumulator = _.union(currentValue))

So, we reduce across zones (which is an array of arrays, where the innermost arrays contain zone names, like zone-0, zone-1). In each step we union it with… nothing. The LoDash union function expects an array of arrays, and returns an array that’s the union of all its inputs. This isn’t how that function is meant to be used, but the behavior from this incorrect usage was that accumulator would end up holding the last element in zones. Which actually worked until recently, because until recently no one was splitting products across zones. When all the inputs were in the same zone, grabbing the last one was just fine.

The code had been like this for years. It was only just recently, as the company expanded, that it became problematic. The fix, at least, was easy- drop the reduce and just union correctly.

[Advertisement]
ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.

Source link

Subscribe to our magazine

━ more like this

Steam baths and seaweed safaris on Sweden’s spa island | Sweden holidays

If you came to stay on the tiny island of Styrsö (steer-shuh) in the Gothenburg archipelago in the late 19th or early 20th century,...

Baggy jeans, workwear and plenty of grit: luxury reimagined at Coach | New York fashion week

New York fashion week is proving a particularly perplexing time for brands as they continue to grapple with a global slowdown, leading many to...

Glossier Cloud Paint Plush Blush Powder Review

Worn alone or with its OG bestie, Cloud Paint's new powder formula gets all the thumbs up from me. The shades are gorgeous (there's...

finding the right queries to fix (part 1)

A few weeks ago, we massively improved the performance of the dashboard & website by optimizing some of our SQL...

Tom Clancy’s Splinter Cell: Deathwatch | Official Trailer | Netflix

Sam Fisher is back and this time, the mission is personal. From the creator of the "John...