Ternary Operator in Kotlin: 5 Main Mistakes Beginners Make

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Ternary Operator in Kotlin: Why It Doesn't Exist and How to Avoid Pitfalls

\n\n

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

\n\n

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\n

Mistake #1: Trying to Insert the Ternary Operator "Head-On"

\n\n

The 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\n

Kotlin 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!

\n\n

How to do it correctly?

\n\n

In Kotlin, if is not an operator, but an expression. It returns a value. Therefore, instead of a ternary, use a regular if-else:

\n\n
// ✅ Correct way: if as an expression\nval max = if (a > b) a else b\n
\n\n

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

\n\n

Mistake #2: Forgetting else When Assigning

\n\n

Since if is an expression, it must return a value. If you write:

\n\n
// ❌ Error: no else, the compiler doesn't know what to return\nval result = if (condition) "Yes"\n
\n\n

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

\n\n

How to do it correctly?

\n\n

Always add else:

\n\n
// ✅ Correct: both branches return a value\nval result = if (condition) "Yes" else "No"\n
\n\n

The exception is if you use if as a statement (without assignment). Then else is not required:

\n\n
// Statement if — else is not needed\nif (condition) {\n    println("Yes")\n}\n
\n\n

Mistake #3: Complex Nested if-else Instead of when

\n\n

When you have more than two options, a chain of if-else if-else turns into "spaghetti". Look at this horror:

\n\n
// ❌ 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\n

The lines jump, your eyes dart around. And if you add a couple more conditions, it will be a nightmare.

\n\n

How to do it correctly?

\n\n

Use when — it's like switch on steroids:

\n\n
// ✅ 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\n

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

\n\n
// when with a range\nval grade = when (score) {\n    i                

Blogs

Book Recommendations