Recommended Code Style Tool?

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.

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.

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.

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

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.

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

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

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.

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

1 Like

Is that work-in-progress Gradle plugin released yet?

@hqzxzwb There’s a Beta that you can try: GitHub - JetBrains/inspection-plugin: Gradle plugin to launch IDEA inspections