re: We don't need a ternary operator VIEW POST

FULL DISCUSSION
 

I agree with with all your points.

But I feel like there's situations where it's arguably clearer than the alternative.

Other commenters have mentioned the functional angle, and that's really it. In many (all?) the languages that have the ternary operator if is a statement (rather than an expression). Make if an expression and ternary has no place in the world:

let a = if cond { true_value } else { false_value };
 

In many (all?) the languages that have the ternary operator if is a statement (rather than an expression).

In Ruby if is an expression but the language also has a ternary operator ¯\(ツ)

 

And in Kotlin! Although Kotlin doesn't have a ternary operator.

It has the Elvis Operator ?: which behaves like C's ternary operator when you omit the middle value... So it's the same as C#'s coallessing operator (??) but it has a groovier syntax!

 

Note, I say we don't need the ternary because it can be replaced with general purpose binary operators. I'm not suggesting to get rid of the functionality, that'd be crazy-talk.

code of conduct - report abuse