Kotlin teams probe static analysis tools to tame method order — but the jury’s still out

The short of it
A team seeking a way to automatically organise — or at least verify — the order of methods and fields in Kotlin recently ran the usual suspects through their paces: detekt, diktat and ktlint. It has been reported that the group already uses ktlint for linting and automatic fixes, and strongly recommends it. But did the tools give them the tidy, mechanical ordering they wanted? Not quite.
Under the hood: same playground, different toys
All three tools work by walking Kotlin’s AST/PSI trees — the same program-structure representation IntelliJ uses. Detekt wraps the compiler’s PSI in its own abstractions; ktlint and diktat use the PSI directly. Rules are implemented as visitor callbacks that inspect nodes (functions, properties, classes) and emit findings with file offsets and human-readable messages. For auto-fixable rules, the tools mutate the AST and serialize the result back to source. In plain English: these linters can rewrite code, because they’re literally editing the code tree.
Order vs. convention: where the tension lives
It has been reported that none of the tools examined support checking method ordering out of the box. Kotlin’s official guidance already discourages mechanical sorting — “put related stuff together,” it says — but the team deliberately questioned that wisdom, arguing that “related” is subjective and a recipe for chaos in large codebases. Allegedly, they considered a strict visibility-order rule as a clear, enforceable alternative. Automatic reordering, however, raises red flags: semantics-preserving? API stability? Git noise? The blog post even trails off mid-thought on whether they’d let a tool rearrange code for them — so the final decision remains murky.
The takeaway
If you want reliable linting and some self-fixing in Kotlin, ktlint is still the safe bet, it has been reported. But if your goal is to impose a deterministic method order across many contributors, there’s no drop-in solution yet — at least not without writing custom rules or risking debates worthy of the next formatting wars. Want a project idea? Building a well-scoped, opt-in ordering plugin that respects semantics and VCS hygiene would make a lot of teams very happy.
Sources: allegro.tech, Hacker News
Comments