Comment on proportional reaction

<- View Parent
PeriodicallyPedantic@lemmy.ca ⁨20⁩ ⁨hours⁩ 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:

var foo=
    x>100 ? bar :
    x>50 ? baz :
    x>10 ? qux :
    quxx;

Over

var foo;
if(x>100) {
    foo=bar;
} else if(x>50) {
    foo=baz;
} else if(x>10) {
    foo=qux;
} else {
    foo=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.

source
Sort:hotnewtop