me personally, i prefer switch case statements for many-value selection, but if ternary works for you, go ham (as long as you don’t happen to be the guy who’s code I keep having to scrub lol)
If there’s more than two branches in the decision tree I’ll default to a if/else or switch/case except if I want to initialise a const to a conditional value, which is one of the places I praise the lord for ternaries.
Switch is good if you only need to compare equals when selecting a value.
Although some languages make it way more powerful, like python match.
but I generally dislike python despite of this, and I generally dislike switch because the syntax and formatting is just too unlike the rest of the languages.
Generally I prefer the clear brevity of:
var foo=
x>100 ? bar :
x>50 ? baz :
x>10 ? qux :
quxx;
Which doesn’t really get any better if you remove the optional (but recommended) braces.
Heck, I even prefer ternary over some variations of switch for equals conditionals, like the one in Java:
var foo;
switch(x) {
case 100:
foo=bar;
break;
case 50:
foo=baz;
break;
case 10:
foo=qux;
break;
default:
foo=quxx;
}
But some languages do switch better than others (like python as previously mentioned), so there are certainly cases where that’d probably be preferable even to me.
PeriodicallyPedantic@lemmy.ca 2 days ago
Hey, when you gotta pick a value from a bunch of options, it’s either if/elseif/else, ternary, switch/case, or a map/dict.
Ternary generally has the easiest to read format of the options, unless you put it all on one line like a crazy person.
guber@lemmy.blahaj.zone 2 days ago
me personally, i prefer switch case statements for many-value selection, but if ternary works for you, go ham (as long as you don’t happen to be the guy who’s code I keep having to scrub lol)
thebestaquaman@lemmy.world 2 days ago
If there’s more than two branches in the decision tree I’ll default to a if/else or switch/case except if I want to initialise a
const
to a conditional value, which is one of the places I praise the lord for ternaries.PeriodicallyPedantic@lemmy.ca 1 day ago
Switch is good if you only need to compare equals when selecting a value.
Although some languages make it way more powerful, like python
match
.but I generally dislike python despite of this, and I generally dislike
switch
because the syntax and formatting is just too unlike the rest of the languages.Generally I prefer the clear brevity of:
Over
Which doesn’t really get any better if you remove the optional (but recommended) braces.
Heck, I even prefer ternary over some variations of
switch
for equals conditionals, like the one in Java:But some languages do
switch
better than others (like python as previously mentioned), so there are certainly cases where that’d probably be preferable even to me.