Eliminate PR Noise with ktfmt

Few things are as frustrating during a code review as seeing a 50-line diff, only to realize that 45 of those lines are just re-ordered imports or adjusted indentation. This is “PR noise”.

In the Kotlin world, ktfmt is one of the best tools to solve this. Developed by Meta, it’s a deterministic formatter that ensures every developer on your team produces the exact same output, regardless of their individual IDE settings.

Why ktfmt?

Unlike some other formatters that allow for a lot of configuration, ktfmt is intentionally opinionated. It is based on the Google Java Formatter, but adapted for Kotlin.

The philosophy is simple: One way to format code. By removing the ability to argue over where a curly brace should go, you free up your brain (and your PR comments) for things that actually matter: logic, architecture, and bugs.

1. IntelliJ Integration: Format on Save

The best way to use ktfmt is to never think about it. By installing the ktfmt IntelliJ plugin, you can have your IDE automatically format your code every time you save.

See https://plugins.jetbrains.com/plugin/14912-ktfmt and https://github.com/facebook/ktfmt#intellij-android-studio-and-other-jetbrains-ides

  1. Install the ktfmt plugin from the IntelliJ Marketplace.
  2. Go to Settings > Other Settings > ktfmt Settings.
  3. Tick Enable ktfmt.
  4. Select one of the styles.
  5. Ensure Reformat on save is enabled in your IDE’s “Actions on Save” settings.

Now, you can type as messily as you want; hitting Cmd + S will snap everything into a clean, consistent structure.

To make intellij format as closely as possible, grab one of the editorconfig files from here https://github.com/facebook/ktfmt/tree/main/docs/editorconfig and make sure to have the editorconfig plugin enabled https://plugins.jetbrains.com/plugin/7294-editorconfig

Java Projects?

If you are working on a Java project instead of Kotlin, you can get the exact same benefits by using the google-java-format plugin and its corresponding Gradle/Maven plugins.

Use the spotless gradle plugin https://github.com/diffplug/spotless/tree/main/plugin-gradle#google-java-format

and the Intellij plugin https://plugins.jetbrains.com/plugin/8527-google-java-format

Note, IntelliJ configuration (files in the .idea folder) can be added to git so it’s contained in the repository for everyone.

2. Gradle Integration: The Source of Truth

While IDE plugins are great for individuals, you need a way to enforce formatting across the whole project. This is where the ktfmt-gradle plugin comes in.

See https://github.com/cortinico/ktfmt-gradle

Add the following to your build.gradle.kts:

plugins {
    id("com.ncorti.ktfmt.gradle") version "0.26.0"
}

ktfmt {
    googleStyle() // or kotlinLangStyle() / facebookStyle()
}

Now you have access to two critical tasks:

  • ./gradlew ktfmtCheck: Validates that all code is correctly formatted (perfect for CI/CD).
  • ./gradlew ktfmtFormat: Re-formats the entire project from the command line.

3. Git Hooks: No More “Oops” Commits

Even with a Gradle plugin, developers sometimes forget to run the check before pushing. You can prevent unformatted code from ever entering your repository by using a Git Hook.

To make Git hooks shareable and version-controlled within your repository, you can use a dedicated .githooks directory. This is a modern approach that allows all team members to automatically use the same hooks.

Here’s how to set it up:

  1. Create a .githooks directory in the root of your repository if it doesn’t already exist.

    mkdir -p .githooks
    
  2. Configure Git to use this directory for hooks. Run this command once per repository:

    git config core.hooksPath .githooks
    

    This command tells Git to look for hooks in .githooks/ instead of the default .git/hooks/.

  3. Create your pre-commit hook script inside the .githooks directory (e.g., .githooks/pre-commit):

    #!/bin/sh
    # Run ktfmt on changed files before committing
    # Use this command
    #   git config core.hooksPath .githooks
    # to tell Git to look for hooks in `.githooks/` instead of the default `.git/hooks/`.
    ./gradlew ktfmtFormat
    git add .
    
  4. Make the hook executable:

    chmod +x .githooks/pre-commit
    

Now, every time you git commit, this script will automatically format any staged Kotlin files using ktfmtFormat and re-add them to your commit. This ensures that all code committed to the repository is consistently formatted. Remember to commit the .githooks directory and its contents so other team members can benefit from it. They will only need to run git config core.hooksPath .githooks once after cloning the repository.

Conclusion

Consistency is a superpower in software development. By adopting ktfmt, you reduce PR noise, allowing your team to move faster and focus on what really matters: building great software.

Try it out
If you’re starting a new Kotlin project, add ktfmt in the first commit. It’s much easier to maintain consistency from the start than to reformat a massive legacy codebase!

What are you using to keep your Kotlin code clean? Let me know on LinkedIn !

If you enjoyed this article, feel free to support my work on Ko-fi!

Support on Ko-fi