Ternary operator


So I came here because I was searching for Ternary operators in Kotlin. (Comming from languages that does have it)…

Considering the arguments, I think that it’s fair it’s not in the language, but I would very much like to see this mentioned in the Docs under FAQ or Comparing to Java, (If it already is, I could not immediately find it, It is mentioned under the control flow section though, here I find it odd)

I certainly have a personal preference leaning towards the ternary operators over the if/else expression examples… But that’s just it, personal preference, and nothing more - I honestly can’t see that turning into a objective argument of any sorts…

Arguments about the readability are silly - both those for and against… There are cases where you can be objective about readability in our field… This is not one of those cases IMO, there is so little difference, so which of the two constructs that is most readable will be highly dependable on who is reading…


If you mean this page: https://kotlinlang.org/docs/reference/comparison-to-java.html

Nothing stopping you from submitting a pull request…


Sorry, can’t let that one go since that is so wrong. Yes you can be totally objective on which is more readable. Saying it is dependent on who is reading has nothing to do with readability. Readability is by defintion objective. You can measure which people understand and how quickly. If one is more easily understood by more people then it is more readable.

You cannot assume that everyone understands the ternary operator. If I showed a line of code to my wife, who knows nothing about programming, which do you think she would understand the ternary operator or an if-else? I think there is no doubt it is the latter. You may counter that she isn’t a programmer but the fact remains if-else will be understandable to more people than the ternary operator.

I am not saying that there shouldn’t be constructs that are obscure until you learn them, but saying that there is no difference in readability here is false.

Sorry, but readabilty and the ternary operator has been a hot button issue for me for over 20 years. Many years ago I purposefully tried not to know the order of ? and : so I would never use the ternary operator because it was less readable and that was long before nice things like when and if expressions.


So your saying that those in this thread that says the ternary operator is more readable to them are wrong? That is just arrogant or ignorant.

You can certainly speak of what a majority of a group would find more readable, but what should that group of people consist of? again we are back to having opinions on how to select that group so we can measure it.

Here if find it totally silly to include your wife, would you think its fair to measure the readability of the french language within a group of Americans and on that conclude that it’s a horrible language because no one or just a few seemed to be able to understand? (although it may win over mandarin in that same group because some words is familiar)

The human brain doesn’t read words, it excels in recognizing patterns, so while a = if b else c; is likely easier to learn to begin with than a ? b : c; because there are patterns that is already known, if your english speaking at least. When learned, my argument is that there is so little difference in which will be more readable to you, that it doesn’t make sense to argue about.


Only in the use of terminology. Humans are very adept at pattern recognition and with repeated exposure humans can learn to recognize, understand, and prefer very cryptic patterns (after all there are actually people who can understand and prefer to code in Perl ;-). I do think it is wrong to associate the term readability to that since readability by a particular individual is kind of a contradiction of terms.

You can ONLY speak about the majority of a group when talking about readability. Readability is all about what is most understandable to the widest audience. It is incorrect to talk about readability in terms of an individual.

You certainly do have to know your target audience. A child’s book is definitely more readable by every objective measure, but you would not write a college textbook that way. But it would be incorrect to say that targeting a more specific is increasing readability.

Not silly at all just an extreme example to illustrate the point. Among people in general as opposed to those who have experience with C-like languages I think we can safely conclude that the ternary operator would be less readable. Among all programmers (not just those coming from C-like languages) I think the same would be true, just not as extreme. Among coders that compete in C obfuscation contests probably no difference.

Readability is not about labeling things with value statements like good, bad, horrible, but about what is the best way to communicate things to other humans. Like Martin Fowler says, “Any fool can write code that a computer can understand. Good programmers write code that humans can understand”

What one could conclude is that if you had something you wanted to communicate to the majority of people living in America that doing so in French would not be an effective way to do that. You would likely want to communicate it in English and probably Spanish to be understood by the most people. That doesn’t deny the fact that there may be people in the US that only speak French, but they are in the minority. If I were trying to communicate to people living in France then I would find I probably would want to use French and English.

Requiring someone to learn == less readable. Sometimes you accept less readable because it gives you more power and expressiveness. E.g. The safe call operator (?.) is less readable for someone coming from many other languages, but it is so powerful that we accept that one has to learn it.

My point was not to argue about ternary operator itself (though I am in the “good riddance” camp), but someone saying that readability is subjective and cannot be measured is a hot button issue for me.

Since Kotlin has if expressions the ternary operator’s only advantage (other than the insignificant number of characters saved) is familiarity to some programmers coming from languages that had it. It offers no new power (and with the use of ? for nullability would likely cause parsing problems). If expressions offer the advantage of being more easily understood by most any programmer,while granted they will be unfamiliar to those programmers who are adept at the ternary operator (which is likely a minority of the group of all programmers and as someone said, “when learned there is so little difference”).


That is funny, just taking a quote from my first post:

That is stating quite the opposite of what your saying I am saying is it not?..

My point is, that IF you measure the readability of the two different constructs presented here, for a group I find suitable, e.g. all new programmers right out of school… My guess is that it will be close to a tie… Unless you have evidence against that… My guess here is as good as yours…

Now IF my guess happens to be correct, then arguments about readability in this particular case become subjective as we lack the evidence either for or against.


No, it is exactly what I am saying you are saying and the whole reason I even replied. You said that the readability of the ternary operator is not a case where one can be objective, that its readability is dependent on who is reading, i.e. it is subjective.

No, that does not make it subjective, it makes it speculative. Without measuring it (or more precisely taking measurements to make statistically significant estimates) we have to speculate. But that is not guesswork, we can make some reasonable, educated guesses based on the evidence of experience.

Let’s break your group down into 4 subsets:

  1. Those that are unexposed and clueless on ternary operator
  2. Those that can understand the ternary operator but it takes them longer to decipher the ternary operator over an if-else
  3. Those that decipher both equally
  4. Those that are actually better at understanding ternary than an if-else

The question then becomes what are the relative sizes of these 4 groups.

For group 1, this group is closer to the example of my wife. Even among non-programmers they are more likely to understand an if-else.

Group 2 is where I fall and I suspect most programmers would fall. And I am not a programmer fresh out of school. I have been programming over 35 years and in Java for over 20 years, but it still takes me slightly longer to mentally parse the ternary operator.

You mentioned having to learn, but all this does is move people from group 1 to group 2, since with no training at all if-else is already more readable. It would take a lot of work to move people from group 1 to group 3 or 4

My estimates on the 4 groups are probably 15%, 70%, 10%, 5%. Those are my own speculations based on my experience, not guesswork. They are not subjective in that they are not based on my own preferences or a particular individual. I could be totally wrong, but I see no evidence to the contrary or to suggest that the numbers even make it anywhere near a tie.

Unless you have strong evidence that the vast majority programmers fall into groups 3 & 4, which would contradict all experience I have, then the idea that the readability is even close to a tie is unfounded.

Even if group 3 were the majority of programmers, Kotlin has if-expressions, which are equally readable to them. The notion that Kotlin should have the ternary operator can only really be made if the majority of programmers fall into group 4 and I am confident that is not the case.

The fact that ternary operator is used sparingly in Java where it actually adds something to the language (since Java does not have is expressions) is further evidence that most programmers fall on the group 1 and group 2 end of the spectrum.


Fair enough, it’s somewhat badly formulated, It’s quite clear you will never get my point though.

However, that is still not saying you can’t be objective about readability in general, which you claim that I said…

It’s not guesswork but we can make guesses?.. What are you saying, that we are guessing or not guessing?..

speculate verb [ I ] (GUESS): to guess possible answers to a question when you do not have enough information to be certain (Cambridge https://dictionary.cambridge.org/dictionary/english/speculate)

Besides, it’s exactly what I am doing… Making an educated guess based on my experience… That experience unfortunately does not match yours…

I have to present evidence of that our arguments are not not based in facts, but rather speculations and guesswork? o.O…

My Experience contradict yours…

Every applicant we get leaves me with that experience, especially drawing on our intern applicants where I get the same picture of our educations as when I was in school my self, which is that close to all (I don’t like saying all even though I have not meet anyone who falls out of that) students within IT are being teached in C like languages as a primary (for the programming part). And with that, knowledge of the ternary operator. (I haven’t meet anyone who didn’t understand that syntax, I can’t say if it has been thought or was intuitive)

I don’t have any evidence beyond my experience, but it sounds like neither does you.


Why don’t invent a new character to replace “?”, for example,
successful() :roll_eyes: “foo” :scream: “bar”

This will keep ? pure and consistent.


This would remove the only advantage the ternary operator has, the fact that people know it from java. In that case you can just use if else.


It still saves 4 characters. :grin:


Pls don’t start this discussion again. I am pretty sure everything has been said about the “but it saves x characters” argument on both sides. So as long as there is not a new one there is no reason to get into it again


In the spirit of making code more readable to everyone, I propose replacing the { } with begin and end keywords as in good old languages like Pascal.

For those confused by ternary operator, I have a question. How do you see generics and extension functions? I’m arogantly guessing that coroutines are definitely a source of confusion stress.


I’m not sure if you really mean this, but even if begin and end where more readable and I am not sure about this, a change like this would break backwards compatibility.

Every language has some necessary constructs which are complex and hard to understand at first. But they are necessary. There is no way you can make coroutines more understandable. The ternary operator on the other hand would be unnecessary as there is an already existing concept which leads to the same result.


Maybe the begin and end can be addedy but not replaced as of breaking channges :joy:

And for me the coroutines are indeed a source of confusion stress.
About the confusion, I think at myself that it indeed is clear what the ternary operator does.
(you do almost the same with nullable if it is null : ?: else: ?.)

(text == null)?     first()   : second()
text          ?.let{first()} ?: second() 

or shorthand:

(text == null)?: second()
text          ?: second() 

(witch is obviously not possible)

I however think there can be beter use-cases for the colon:

  • Making it an oprator fun for creating a range that can go both up and down
  • The equivalent of to for a mapEntry, such that the forEach with receiver now can refer to key and value instead of first and second

And in kotlin, I always will associate the questionmark with nullable and I hope it stays that way.


Kotlin does not need ternary. Initially I wanted to vote for adding it. But now I think that this

    llMain.visibility = if (b) VISIBLE else GONE

looks better that this

  llMain.visibility = b ? VISIBLE : GONE


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.”