https://kotlinlang.org logo
#general-advice
Title
# general-advice
x

xxfast

11/02/2023, 5:31 AM
Hi All! What tools do you guys use for formatting kotlin? Basically I want something that 1. Is very-deterministic 2. Can quickly fail a build if it detects inconsistent formatting 3. Configurable with custom rules if needed Did some research and found formatters like #ktlint, #ktfmt, diktat and integrations like #detekt, spotless that can use these formatters. Curious to know if anyone has experience to share using any of these and what works best for you I'm sure this question has been asked before, but the searches are not yielding what i'm looking for exactly. Feel free to point me to a relevant thread, if any
Related reddit post here: https://www.reddit.com/r/Kotlin/comments/10v042b/what_are_the_good_code_formatters_for_kotlin/ Which seems to favour #ktfmt as I'm looking for something more deterministic
j

Jakub Gwóźdź

11/02/2023, 6:53 AM
In my team we’re using detekt with a few custom settings and it does it’s job for us for almost two years now. Pros: •
./gradlew detekt --auto-correct
does the reformat to eliminate problems whenever possible • IDEA formatting also can be configured to format code so detekt is happy • You can tweak config a little so if you have a team member who will protest against some minor quirks in code style, you can compromise and adapt so everyone is happy • It’s rather quick to adapt to new language features, although to be fair, it’s a long time since we had it in Kotlin, isn’t it. •
./gradlew build
on cicd will fail-fast if you push ugly code because you forget to check it locally. Cons: •
./gradlew build
on cicd will fail-fast if you push ugly code because you forget to check it locally. • Isn’t too quick (gradle plugin takes time to start and process all files, but to be fair it seems to be caching things from run to run)
x

xxfast

11/02/2023, 6:56 AM
one of the comments from the reddit thread above (from 9 months ago) claims ktlint is not that deterministic. Is that really the case? Have you ran into such issues recently?
j

Jakub Gwóźdź

11/02/2023, 7:01 AM
But what does it mean? If it formats same thing one time this way and then reformats in another? Nope. It never reformats code that it accepted earlier, at least I don’t think we ever noticed anything like that.
x

xxfast

11/02/2023, 7:02 AM
Yup, as long as it formats the same way every time - given the same input - should be good enough deterministic for me
Thanks for your input
👍 1
j

Jakub Gwóźdź

11/02/2023, 7:14 AM
To be fair, new versions of detekt might introduce new rules (although more often in code structuring than format) so when updating that, you may be forced to do some additional changes to you existing codebase
x

xxfast

11/02/2023, 7:25 AM
How similar are the default rules from intelij built-in ones?
j

Jakub Gwóźdź

11/02/2023, 7:36 AM
That question I can’t answer now as I don’t even remember at this point 🙂
s

Stylianos Gakis

11/02/2023, 8:02 AM
one of the comments from the reddit thread above (from 9 months ago) claims ktlint is not that deterministic.
Version 1.0 which was released just recently should be much more deterministic
The concerns about determinism were basically about what happens when a line exceeds the max width, and how it breaks over to the next line. That was not that deterministic before I believe
👍 1
x

xxfast

11/02/2023, 8:34 PM
Thanks for the insight everyone. Looks like detekt with ktlint seems to be the popular opinion