this post was submitted on 14 Jul 2023
375 points (99.5% liked)

Programmer Humor

32380 readers
1208 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] kibiz0r@lemmy.world 162 points 1 year ago (1 children)

To be fair: If you are chaining ternary expressions, you deserve to suffer whatever pain the language happens to inflict upon you tenfold.

[–] Serdan@lemm.ee 16 points 1 year ago (2 children)

Why?

It's perfectly readable.

[–] lowleveldata@programming.dev 38 points 1 year ago (2 children)

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

[–] rikudou@lemmings.world 3 points 1 year ago

Match is even better, short and sweet.

[–] Serdan@lemm.ee 1 points 1 year ago (1 children)

Ternary expressions aren't switches though

[–] Kryomaani@sopuli.xyz 19 points 1 year ago (1 children)

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

[–] Serdan@lemm.ee 5 points 1 year ago* (last edited 1 year ago) (1 children)

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 4 points 1 year ago* (last edited 1 year ago) (1 children)

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 3 points 1 year ago (1 children)

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 3 points 1 year ago

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

[–] deaf_fish@lemm.ee 1 points 1 year ago (1 children)

There is usually a safer and more readable way to do what you want to do by chaining ternaries in most languages.

[–] Serdan@lemm.ee 4 points 1 year ago (1 children)
[–] deaf_fish@lemm.ee 2 points 1 year ago* (last edited 1 year ago) (1 children)

Well, if you assume ternary operations work the same in PHP as in c and attempted to write the code demoed by this meme. You would end up with unexpected behavior. Maybe I should have said unexpected behavior instead of unsafe behavior.

[–] Serdan@lemm.ee 2 points 1 year ago (1 children)

PHP is the only language in existence with a left associative ternary operator. Ignoring PHP, the operator has worked exactly the same way for decades. And even PHP has now fixed the operator.

I don't think it's reasonable to avoid a very commonly supported pattern just because a single badly designed language implemented it wrong.

[–] deaf_fish@lemm.ee 2 points 1 year ago (1 children)

Okay, even if I give you the unexpected behavior point. The readability problem remains. Switch statements or tables will work just fine and are easier to read.

To be clear, I am fine with single ternary operations. I think nested ternary operations are harder to read and follow.

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

I agree you should use a switch where applicable, but ternaries are the expression equivalent of if-else statements. If I have two conditions and a default, and each branch simply evaluates to a value of the same type, I'll probably just use a ternary.