Ternary operator


Even as an option this would be breaking, because I am 100% sure there are people out there (me included) who have functions named begin and end :stuck_out_tongue:

And srsly “Coroutines are confusing” is not an argument for the ternary operator. Yes they might be, but there is no better option. For the ternary operator there is an option and I would argue a better one.


I made a summary of the whole topic and the arguments (named once or over and over).
But before the discussion, its important to know what is possible:

(success) ? first() : second()  //possible
(success) ?: second () // not possible as it checks if success is null

Next, I didn’t want to chain facts between opinions, as it makes the opinions look like facts.
I couldn’t however distinguish all the facts from opinions, so I could have made some facts opinions and vice versa.
The opionions are cursive.
Next, I tried to be unopinionated, but I’m against the ternary operator. This can be reflected below.

  • A lot of languages have it which:
    - makes the conversion from those languages harder
    - means that the construction should be unlearned by those programmers.
  • It looks less verbose
  • Its shorter
    • but its at most 6 chars shorter
    • being short is not a goal of kotlin
  • The choice should be left upon the programmers
    • but more concepts for the same thing is harder to learn.


  • for (at least) beginning programmers: it is hard to learn.
  • Its not a priority (at the moment) so the attention can be focussed on more important things
  • The questionmark in Kotlin is associated with null:
    - the shorthand if/else ?: is not even possible.
    - the code can be interpreted different:
    success? foo : bar can be interpreted as if(success == null) foo else bar
  • It’s not needed as:
    • If is already an expression
    • you can create a comparable function yourself
      infix fun <T : Any> Boolean.t(value: T) : T? = if(this) value else null
      (I however recommend you not to use this)

When something is not on the list, please respond. Otherwise, we just keep repeating ourselves.


Sorry to take so long to get back to you. You completely ignored my distinction between groups 2, 3, and 4 which all fall into your group about being “teached” a C like language. I didn’t contradict the assertion that many are taught a C-like language and estimated the figure at 90%. But just because they learned Java or similar language does not mean that ternary operator is easier to read to them.

There is plenty of other evidence. As I have said the fact that ternary operator is used so infrequently in Java code even though it actually has a reason to exist in Java is one piece of evidence. Googling and seeing how many peope say don’t use it because it is harder to read is also good evidence.


I, for one, emphatically throw my hat into the ring in favor of the standard C-style ternary operator. While I’m not averse to shaving away questionably or unnecessary functionality that does not make sense in the new language (such as old for-style loops), this is just change for the sake of change. It’s no less ugly than the traditional method, it’s bulkier making it harder to cleanly fit on a single line, but also lopsided in a way that doesn’t lend well to splitting up over multiple lines (at least for that there are when statements). And it’s honestly rather confusing in an extremely subtle way that would, if anything, only trip up beginner programmers.

Someone who doesn’t know what a ternary operator is will see “x = a ? b : c” and think “I don’t know what this is, I should look it up.” They see x = if( a) b else c they might say the same thing or they might say “What is this, some sort of predicate? Is x a value or a command path? I know what an if-else block is, is this a standard if else block? What does assigning that even do? Can I do that with other control structures?”

The fact that even IntelliJ IDEA has problems formatting the if-else syntax mid-editing because it thinks its a malformed if-else block until it’s completely finished should tell you that you’re probably going about this the wrong way.

Frankly, if you don’t have the balls to remove the ternary operator entirely, don’t give it some confusing coat of paint to make it “more obvious.”