Back

Switching from Java to Kotlin: the most common mistake

In this article, we’ll discuss a common mistake beginners make when switching from Java to Kotlin for the first time. I often notice that people who have recently switched from Java to Kotlin become fascinated by its idiomaticity and start using it where they shouldn’t. 
 

For example:


.         

 

What’s wrong here?

  • It’s unclear what the function returns.
  • It’s not obvious that the return value is nullable.
  • Without a hint, it’s unclear what the “let” applies to.
  • Large nesting depth.

There is also a logical error here, that has been made due to the difficulty of perceiving this construction — there is no check that “tags” is not empty.

You don’t need StringUtils either, Kotlin has isBlank() for strings.

How can you fix it?

  • Explicitly specify the return type.
  • Rewrite top-level logic in an imperative style