Ternary operator

Me too! I’m glad others are selling Kotlin on this. It surprises me that people still prefer the ternary operator over if-expressions after they’ve tried them. Would those people also prefer Python’s ternary operator, “a if condition else b”?

I feel this topic has been well summarized several times by multiple people over the last year. Should this topic be closed?


And you ignored everything I said that refutes your claim to make a nonsensical reply.

Kotlin keywords are based on English so yes I of course stuck with English just as you did. Are you claiming there is some non-English natural language where one routinely says “it is raining? drive to work: bike to work”

1 Like

I’m claiming that ternary is quite familiar concept for most programmers either from other langs from largely grown C-family or natural languages and was never broken. Unless of course deep nesting of ternary. Which is also true for if and any other statement since 5-6 levels of nested ifs is not so readable too.
But thats ok cause IDE can fix it in a moment and replace ternary with ifs.
And lack of ternary is especially annoying on multi-language projects.
I’m also claiming that sticking to the rule “? always relate to null” adds nothing to kotlin.

1 Like

as actual proof of your words “I’m claiming that ternary is quite familiar concept for most programmers” is swift lang: swift uses ? for optional chaining(Optional Chaining — The Swift Programming Language (Swift 5.7)) like this:
if let roomCount = john.residence?.numberOfRooms {}
and swift uses ? for ternary operator.
But, need kotlin designers to add ternary, they would already add it in kotlin.

Actually that was not the argument you were making. That is the ONLY valid argument for ternary operator, but is not a particularly compelling one. There is plenty familiar to Java programmers that were jettisoned in Koitlin for good reason.

For those not coming from a C-based language it is very unfamiliar. if-else is familiar to both.

And we have to ask why did it exist in those C-derivative languages? Because it filled an important role because if was a statement in those languages and not an expression. Since Kotlin eliminated that problem and if is an expression there is no need to use the confusing hack of the ternary operator.

That was the actual claim you did make but did not adequately support and is not valid. Unless you can produce a natural language that uses ? : as a ternary expression then your claim is invalid.

Actually it was always broken and a bad hack. See: ternary operator considered harmful - Google Search

Adding the ternary operator would add nothing to Kotlin as it would be a redundant construct that is non-obvious to those unfamiliar with it. It would detract from Kotlin in that it adds confusion by giving an alternate meaning to ?.

And once again they did that because, like Java, C and other languages, they made “if” a STATEMENT not an EXPRESSION. When if is an expression as it is in Kotlin then the ternary operator would just be a an ugly, non-obvious alternative to the much more clear if-expression.

I’ve created a topic to discuss closing this topic and the companion object topic: Please close the mega topics (ternary and companion objects)


Then why language designers adding it over and over again? Swift, groovy, js, C#. I bet there are many more

I never claimed that there are natural languages with that exact syntax, it was you imagination. I claimed that in natural languages there are similar constructs, and anyone can recognize this constructs in ternary.

Why ruin all the fun?

And who would be that people writing on jvm-based lang, stated to be better java, stated to be targeting java developers, and unfamiliar with ternary?
Anyway its no big deal to grasp it

Reasons to not add the ternary operator:

  • It doesn’t add any new functionality
  • If-expressions work okay as they are now
  • It can lead to unclear code when chaining ternaries
  • It can lead to unclear code when using long expressions
  • It is difficult to understand for those new to programming
  • It clashes with Nullability’s use of the question mark
  • It clashes with potential uses of the colon
  • The minus 100 point rule

Reasons to add the ternary operator:

  • It’s more concise (shorter character-wise)
  • It’s expected by users coming from other languages
  • It’s understood by users coming from other languages
  • It does not clash with Nullability’s question mark (see other languages)
  • It doesn’t limit other future uses of the colon

Reasons to end the fun:


Because they failed to fix the fundamental problem that if was a statement rather than an expression. So they just fell into copying the ugly alternative rather than fix the problem.

This is what you said:

And yet you provided no examples of this. You provided an example that agrees with Kotlin’s use of ? for nullability. I provided an example where natural language uses an if-else style of syntax.

But I agree with arocnies, stop this thread.

I know what I said :slight_smile:

And yet there are modern languages with both ternary and if expressions
Maybe there was no problem at all? And ternary was added for convenience

Apparently not since you denied saying it.

Are there, really? The primary C-based languages Java, Javascript, C#, Swift, C++, and C for example do not have if EXPRESSIONS, they have if STATEMENTS. To be clear an if expression allows you to say:

result = if(a < b) c else d

That is not allowed in any of the above mentioned languages. Kotlin does allow this and thus there is no need for the ternary operator.

Ruby is the only language I know of that has both and I dismiss any idea that we should look at Ruby for best programming practices.

For reference see Wikipedia: ?: - Wikipedia

  1. how about a bit another syntax
    condition ?? onTrue : onElse
    condition ?? onTrue ?: onFalse
    condition => onTrue ? onFalse
  2. temporal solution:
    operator fun Boolean.rem(t: T) = if (this) t else null
    val t1 = true % 1 ?: 2
    val t2 = false % 1 ?: 2

This removes the only advantage the ternary operator has, the old, familiar syntax.

1 Like

If is not a goal of kotlin save characters, then why do not use “function” instead of “fun” for define a function? For me, the ternary operator provides a short and concise way to declare a if else, because as say @nguyenxndaidev it is only a feature that is optional to use, if you consider that the if else sentence is more clear in your case, then use it but if you want to use the ternary operator I think that it should be allowed.
I do not understand why limit the language.
An example of why I prefer ternary operator over if else.

val result = if (expressionTooLong....................................................)

With ternary operator you could do:

val result = if (expressionTooLong....................................................)
    ? resultA
    : resultB

That is more clear at least for me.


Check out this topic: Please close the mega topics (ternary and companion objects)

This recent post is a post in response to the 8th post in this thread and is a really good example of how a mega thread isn’t able to end naturally:

The topics aren’t ending naturally
These topics are interesting and people will have an opinion on them. This is great until the topic becomes a place to petition for/against a position. Even though everything has been said, another user will come in support/against the feature of discussion without communicating anything new.

The original link is far enough up in the thread history that there’s a good chance people won’t see it and resume commenting on this thread.

Reposting hoping this link remains at the bottom :slight_smile:


Thank you, we have decided to close this topic. You can read the quick summary here.