Ternary Operator in Kotlin: Why It Doesn't Exist and How to Avoid Pitfalls
\n\nIf you came to Kotlin from Java, C#, or JavaScript, you've probably at least once caught yourself thinking: "Where is my beloved ternary operator condition ? value1 : value2?" Spoiler: it doesn't exist in Kotlin. And this is not a bug, but a feature. The language designers decided that the classic ternary is an unnecessary entity that only complicates code readability, especially when there are more than two nested conditions.
But the absence of a ternary operator is not a problem. Kotlin offers much more flexible and safer alternatives. However, beginners (and even experienced developers) often make the same mistakes trying to emulate the familiar construct. In this article, we'll break down the top 5 mistakes when working with conditional expressions in Kotlin and show how to avoid them. Let's go!
\n\nMistake #1: Trying to Insert the Ternary Operator "Head-On"
\n\nThe most common mistake is writing code in the Java style:
\n\n// ❌ Error: this syntax does not exist in Kotlin\nval max = (a > b) ? a : b\n\n\nKotlin simply will not compile such code. You will get an error: Unresolved reference: ?. Many developers freeze up and start looking for libraries or plugins that "add" the ternary operator. Don't do this!
How to do it correctly?
\n\nIn Kotlin, if is not an operator, but an expression. It returns a value. Therefore, instead of a ternary, use a regular if-else:
// ✅ Correct way: if as an expression\nval max = if (a > b) a else b\n\n\nThe code is even easier to read than the classic ternary, especially if the condition is complex. Remember: if in Kotlin always returns the last value in each branch.
Mistake #2: Forgetting else When Assigning
\n\nSince if is an expression, it must return a value. If you write:
// ❌ Error: no else, the compiler doesn't know what to return\nval result = if (condition) "Yes"\n\n\nThe compiler will give an error: All branches of 'if' expression are required to return a value. This is logical: what should be in result if the condition is false? null? An empty string? The compiler doesn't guess.
How to do it correctly?
\n\nAlways add else:
// ✅ Correct: both branches return a value\nval result = if (condition) "Yes" else "No"\n\n\nThe exception is if you use if as a statement (without assignment). Then else is not required:
// Statement if — else is not needed\nif (condition) {\n println("Yes")\n}\n\n\nMistake #3: Complex Nested if-else Instead of when
\n\nWhen you have more than two options, a chain of if-else if-else turns into "spaghetti". Look at this horror:
// ❌ Bad: nested if-else is hard to read\nval status = if (code == 200) "OK"\n else if (code == 404) "Not Found"\n else if (code == 500) "Server Error"\n else "Unknown"\n\n\nThe lines jump, your eyes dart around. And if you add a couple more conditions, it will be a nightmare.
\n\nHow to do it correctly?
\n\nUse when — it's like switch on steroids:
// ✅ Great: when reads like a table\nval status = when (code) {\n 200 -> "OK"\n 404 -> "Not Found"\n 500 -> "Server Error"\n else -> "Unknown"\n}\n\n\nwhen in Kotlin is also an expression, so it can return a value. And it is much clearer than a chain of if-else. Additionally, when supports complex conditions: ranges, types, arbitrary expressions.
// when with a range\nval grade = when (score) {\n i