Today's anonymous submitter has this to say about this code: "It works fine, it's just... clever."

I'm not certain about the relative cleverness of this solution, myself.

switch (true) { case (d <= 15000): m.values[0]++; break; case (d > 15000 && d <= 30000): m.values[1]++; break; case (d > 30000 && d <= 45000): m.values[2]++; break; default: m.values[3]++; break; }

This JavaScript lives in a web dashboard for monitoring an internal system. Like most such systems, it was slapped together in a rush with no real thought, and nobody actually cares too much about it so long as all the indicator lights on the dashboard stay green.

The obvious thing about this switch is that it should be an if. Arguably, it should even be a loop, which would allow you to iterate across a series of breakpoints, so that you could have a histogram with an arbitrary number of buckets. Perhaps that's premature abstraction, but at least this should be an if.

I'd suggest that maybe they were trying to play some code-golf, since case (...) : is shorter than else if (...) {, but all those savings are lost if you count the break.

That said, I have an idea to make it worse. This is JavaScript, so you could actually do this instead:

m.values[0] += d <= 15000; m.values[1] += d > 15000 && d <= 30000; m.values[2] += d > 30000 && d <= 45000; m.values[3] += d > 45000;

It's less efficient, as you check every case every time, but look at how much more compact it is. And this is for a web page, nobody cares about efficiency in web development anymore. Just look at how clever it is in its (ab)use of the JavaScript type system!

May the gods save us from "clever" programmers.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!