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. Feel free to share your experience in the comments.

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.

Share in the comments what mistakes you faced when switching from Java to Kotlin for the first time.

--

--

Since 2004, we’ve been doing our part in changing the entertainment & tech space. Learn more at http://bit.ly/3XWJemV

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
FunCorp

Since 2004, we’ve been doing our part in changing the entertainment & tech space. Learn more at http://bit.ly/3XWJemV