343
submitted 1 year ago* (last edited 1 year ago) by AlmightySnoo@lemmy.world to c/programmerhumor@lemmy.ml
you are viewing a single comment's thread
view the rest of the comments
[-] lowleveldata@programming.dev 36 points 1 year ago

It is sort of readable. A switch is "perfectly" readable for switching.

[-] rikudou 3 points 1 year ago

Match is even better, short and sweet.

[-] Serdan@lemm.ee -5 points 1 year ago

Ternary expressions aren't switches though

[-] Kryomaani@sopuli.xyz 18 points 1 year ago

Which is exactly why you shouldn't be using them in a situation that clearly calls for a switch.

[-] Serdan@lemm.ee 4 points 1 year ago* (last edited 1 year ago)

In the given example I'd probably use a switch / match expression, but ternaries are usually more flexible than switches and I don't think it's an issue to write a nested ternary instead of if else statements.

[-] lowleveldata@programming.dev 2 points 1 year ago* (last edited 1 year ago)

ternaries are usually more flexible than switches

Which is bad for readability because the reader need to manually compute it to see whether it's doing simple switching or not. Also it adds the question of "Why did the author use a nested ternary instead of a switch? Was it meant to do more but it got left out unintentionally?"

[-] Serdan@lemm.ee 1 points 1 year ago

Yes, you need to read code to understand it. If else statements can also do the job of a switch, so the exact same argument applies.

[-] lowleveldata@programming.dev 2 points 1 year ago

The point is I don't need to read a switch statement to know that it is a switch

this post was submitted on 14 Jul 2023
343 points (91.3% liked)

Programmer Humor

32443 readers
912 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS