News for hackers. Stuff that matters.
Re:Using the switch(true) pattern in JavaScript by elyseum on Monday April 12, 2021 @10:12AM (#26779648)
A good example of: just because you can, it doesn’t mean you should. Yes, the if/else sequence is a bit more unstructured in terms of code layout, but chances the next developer browsing the code will instantly know what it does will be much higher.
Re:Using the switch(true) pattern in JavaScript by csmattryder on Monday April 12, 2021 @10:32AM (#26779866)
Spare a thought for the junior/mid-level devs who don't understand clever patterns, and the seniors who haven't read the same blog posts as the implementor of the switch(true).

Completely agree, it's like writing readable prose, understand your audience.

That said, give me proper pattern matching in JS. Hard to live with languages that haven't caught up yet.

Re:Using the switch(true) pattern in JavaScript by nicoburns on Monday April 12, 2021 @10:43AM (#26779965)
> That said, give me proper pattern matching in JS. Hard to live with languages that haven't caught up yet.

I really want this. Along with switch being as expression. Seems like something that JavaScript is obviously missing.

Re:Using the switch(true) pattern in JavaScript by deergomoo on Monday April 12, 2021 @06:03PM (#26785741)
> Along with switch being as expression

Even PHP has this now, albeit with a different keyword (match) due to existing switch semantics being pretty bad

Re:Using the switch(true) pattern in JavaScript by jessaustin on Monday April 12, 2021 @05:23PM (#26785383)
I really want... switch being as expression.

Unfortunately JavaScript still hasn't caught up with CoffeeScript.

Re:Using the switch(true) pattern in JavaScript by throwanem on Monday April 12, 2021 @07:29PM (#26786430)
Of course, you can't evaluate prose in a REPL and see for yourself what it does.

I agree that overly clever code is always to be avoided. But is this really overly clever? I wouldn't expect an engineer early in their career necessarily to understand on sight what it does. But I might worry a little for one who couldn't figure it out through experiment.

Re:Using the switch(true) pattern in JavaScript by TheHalfDeafChef on Monday April 12, 2021 @10:45AM (#26779990)
I consider myself an advanced-beginner programmer untested in the real world (looking for my first coding job though!), and I understood it relatively quickly. It does appear more concise than many if-else statements. After the initial “what?”, I was able to quickly parse through it. I think this is faster than using the standard form.

[edit: fixed typo]

Re:Using the switch(true) pattern in JavaScript by t-writescode on Monday April 12, 2021 @03:46PM (#26784225)
A lot of the code you're going to have to be dealing with in the wild is going to look fine, until it breaks at 2am on a Saturday and you're 3 drinks in and production is down.

You're going to hate your former cleverness then.

Re:Using the switch(true) pattern in JavaScript by TeMPOraL on Monday April 12, 2021 @06:21PM (#26785913)
There should be a rule that forbids applying this reasoning to code that isn't triple-nested template SFINAE mess, or a code-generating-code-generating-code-generating-code Lisp macro.

This pattern isn't "cleverness", it's just a readability-improving trick that's cheap to figure out. The hundred seconds you spend on it seeing it for the first time is a one-time cost.

Re:Using the switch(true) pattern in JavaScript by tantalor on Monday April 12, 2021 @03:49PM (#26784247)
That's why I abstain when I'm oncall (or backup).

That's what the oncall bonus pay is for.

Re:Using the switch(true) pattern in JavaScript by t-writescode on Monday April 12, 2021 @03:55PM (#26784332)
Replace alcohol with 2 hours of sleep, then :)
Re:Using the switch(true) pattern in JavaScript by arielserafini on Monday April 12, 2021 @11:29AM (#26780466)
"faster" in which respect?
Re:Using the switch(true) pattern in JavaScript by TheHalfDeafChef on Monday April 12, 2021 @04:00PM (#26784404)
Faster to parse as a developer. I can't speak to whether computationally it is faster.

Additionally, doesn't this somewhat also bring in the methodology of dealing with the error cases first?

Re:Using the switch(true) pattern in JavaScript by TeMPOraL on Monday April 12, 2021 @06:11PM (#26785810)
The only problem here is that you have to do this in the first place. This problem has been solved some 60 years ago with Lisp and `cond` construct, which is even older than `if/else if/else`! Alas, our industry likes to forget history.

That said, this isn't some kind of high-level hackery. If it's cleaner than if/else if chain, you should use it.

Programming is a profession. A professional is expected to learn and grow. The right answer to seeing a code construct one doesn't understand isn't throwing hands up in the air, but spending the few minutes necessary to figure out what it does.