Why?

It’s perfectly readable.

It is sort of readable. A switch is “perfectly” readable for switching.

Rikudou_Sage
link
fedilink
31Y

Match is even better, short and sweet.

Ternary expressions aren’t switches though

@Kryomaani@sopuli.xyz
link
fedilink
English
161Y

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

@Serdan@lemm.ee
link
fedilink
English
2
edit-2
1Y

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
link
fedilink
English
2
edit-2
1Y

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
link
fedilink
English
11Y

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.

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

deaf_fish
link
fedilink
-11Y

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

How is it unsafe?

deaf_fish
link
fedilink
1
edit-2
1Y

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.

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
link
fedilink
11Y

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.

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.

Create a post

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 1 user online
  • 120 users / day
  • 257 users / week
  • 744 users / month
  • 3.72K users / 6 months
  • 1 subscriber
  • 1.47K Posts
  • 32.2K Comments
  • Modlog