Hi All! What tools do you guys use for formatting ...
# general-advice
x
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
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
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
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
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
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
How similar are the default rules from intelij built-in ones?
j
That question I can’t answer now as I don’t even remember at this point 🙂
s
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
Thanks for the insight everyone. Looks like detekt with ktlint seems to be the popular opinion