Recommended Code Style Tool?


#1

Interested to know what code style tool people recommend for Kotlin? Having difficulty deciding whether to use detekt or ktlint for applying a code style to Kotlin code.


#2

Gradle Inc seem to be recommending ktlint.

  • @mikehearn - Does Corda use a code style tool for Kotlin?
  • @yole - What code style tool are JetBrains using for Kotlin?
  • @JakeWharton - Does Square Inc use a code style tool for Kotlin?
  • @sdeleuze - Does Pivotal use a code style tool for Kotlin?

Would be interesting to know what code style tool Google are using for Kotlin.


#3

Actually this question doesn’t make sense without clarifying what you need a static analysis tool for. Are you looking to avoid unnecessarily verbose code, enforce consistent code formatting, avoid security defects, check your code metrics, or what? All these tasks are solved with static analysis tools, and each of these tasks is handled by a different tool in the Java world.

At JetBrains, we don’t use any tools other than IntelliJ IDEA’s built-in inspections.


#4

Really meant a code style tool. One where the user sets up some code style rules/guidelines, or uses the defaults which are applied to a Kotlin project. IntelliJ has a similar facility for Kotlin however it is very limited compared to the Java version.

Kotlin Code Style Settings

Java Code Style Settings


#5

The difference in code style settings is intentional to a degree. We prefer to see Kotlin code using consistent coding styles (similar to how it happens in the Go community, or in Python with PEP8), and not to support infinite configuration possibilities.


#6

If your using intellij as an IDE then maybe you want to check out the format.sh script inside the bin file which runs intellij headless and formats your code. Additionally you can run detekt to check if your methods are not too complex. (Note: I’m the maintainer of detekt.)


#7

@napperley you might also want to take a look at Linting your Kotlin Builds - IntelliJ IDEA Inspections by Hadi Hariri from JetBrains team.


#8

Some very good suggestions made by @abosch and @shyiko :+1:. Also of note is that IntelliJ can apply a code style to Kotlin code which is useful when you are dealing with a small sized project.


#9

The 1.1.50+ IntelliJ IDEA plugin supports checking the Kotlin code style against the settings configured for the project.

Currently this is only an IDE feature, but there’s a work-in-progress Gradle plugin with the same functionality (not available yet).

image