9 rules to help Java developers writing Kotlin idiomatic code

Hello Java/Kotlin developers,

As an experienced Java developer in the process of adopting Kotlin, or even as a developer discovering Kotlin, you should appreciate the new rules we’ve just introduced. We’ve noticed that many Kotlin developers are actually former Java developers transitioning to Kotlin, and in doing so, they may make the mistake of writing Kotlin code as if they were still writing Java code. They apply Java idioms to Kotlin code, even though Kotlin has a more elegant and concise way of achieving the same goal. While this approach may work, it prevents from taking full advantage of Kotlin’s cutting-edge features.

Here is the list of Kotlin rules added to help Java developers writing Kotlin idiomatic code:

  • S6510: “return” statements should be lifted before “if” or “when” statement
  • S6511: “When” statements should be used instead of chained “if” statements
  • S6508: “Unit” should be used instead of “Void”
  • S6518: Element access should use indexed access operators
  • S6512: Getter and setter pattern should use property getters and setters
  • S6519: Structural equality tests should use “==” or “!=”
  • S6517: Single-function interfaces should be functional interfaces
  • S6516: Functional interface implementations should use lambda expressions
  • S6514: Delegator pattern should use “by” clause

This is available now on SonarCloud and will be included in SonarQube 10.1

Alex

5 Likes

A post was split to a new topic: How to refactor what is reported by S6517?

I was doing the exact same thing. But this post saved my day.

Thank you

1 Like