The Suppressenator

Kotlin Programmering Development

Uzi Landsmann

Systemutvecklare

A way to visualize the usage of your @Suppress annotations

Are you using a static code analyzer like Detekt with your team’s Kotlin code? So do we. It is a good idea because it is a safeguard that helps your team maintain its code style and helps align the code produced by the team to a certain quality level. However, we sometimes find some of the code-style rules cumbersome and annoying. Perhaps we tell ourselves that the limit of six parameters in a function is too small in certain situations, or that a class sometimes needs to contain more than eleven methods. Instead of adjusting the limits of the rules in the configuration file, though, we often tend to use the @Suppress annotation to ignore them. Temporarily, of course. We will fix it as soon as we have some time over, we tell ourselves.

 

If you recognize yourself in the description above, please read on to see how we intend to deal with the problem of the increasing number of annotations that suppress the rules of code style. (And if you don’t, you’re welcome to read on anyway)

 

 

We are almost always in some kind of crunch mode, aren’t we? There’s always a deadline that needs to be met or a feature that needs to be implemented yesterday. More often than not, code quality suffers from this situation. And if you’re using a static code analyzer in your IDE or your build pipelines, it is often blinking red and warning you about the transgressions you’ve made, ignoring the code style rules. It is then that we often use the @Suppress annotation.

Our repositories are overflowing with them, and we often find ourselves asking each other why we use these tools anyway, and what they are good for since we can never find the time to adjust the rules and find an adequate set of limits that would meet our needs. This means that we constantly suppress more rule violations and find the tools more and more annoying.

 

A wise developer once told me that when facing a complex problem, the first step is to visualize it. This is what I intended to do when we tried dealing with the increasing number of @Suppress annotations in our code. The idea was that if we want to reduce that number, we need to understand which rules we often suppress and prioritize adjusting them first. It was just then that I saw the new Kotlin notebook plugin in Intellij and thought it might be an excellent match and decided to give it a try. This resulted in the Suppressenator notebook found in this GitHub repository.

 

As an example, have a look at this not-too-great piece of code:

 

package example

class Example {

    @Suppress("LongParameterList", "FunctionOnlyReturningConstant", "MagicNumber")
    fun methodWithLongParameterListAndOnlyReturningConstant(
        @SuppressWarnings("UnusedPrivateMember") a: Int,
        @SuppressWarnings("UnusedPrivateMember") b: Int,
        @SuppressWarnings("UnusedPrivateMember") c: Int,
        @SuppressWarnings("UnusedPrivateMember") d: Int,
        @SuppressWarnings("UnusedPrivateMember") e: Int,
        @SuppressWarnings("UnusedPrivateMember") f: Int,
    ): Int {
        return 123 + 987
    }

    @Suppress("LongParameterList", "UnusedPrivateMember", "MagicNumber", "MaxLineLength")
    fun anotherMethodWithLongParameterList(
        aa: String,
        bb: String,
        cc: String,
        dd: String,
        ee: String,
        ff: String,
    ): String {
        return aa + bb + 11 + "a very long sentence which makes this line way way way way way way way way way way way too long"
    }
}

 

 

Analyzing the annotations in this class using the notebook yields the following chart:

 

Supress1

A visualization of the Suppress annotations in some really bad piece of code, both created by the author.

 

 

The notebook traverses through the Kotlin files under the configured root directory and collects the annotations it finds in them. It then uses a simple bar chart to visualize them, as seen in the image above.

 

If you want to use the notebook in your code and visualize the annotations in there, follow these simple instructions:

  1. Install the Kotlin notebook plugin in your Intellij IDEA
  2. Copy the suppressenator.ipynb file from this repository and put it in your repository
  3. Adjust the rootDirectory to match the root of your code
  4. Press the Run All button on the top of the notebook
  5. Voila! The notebook will attempt to find all your @Suppressand @SuppressWarning annotations and visualize them at the bottom.

Running the notebook in our largest repository led to quite a depressing realization, that we overuse many annotations and need to spend some time both refactoring the code and adjusting some of the rules in our Detekt configuration file. So in order not to despair, I thought it could be interesting to see what other repositories look like when using the notebook. It turns out we’re not alone. Here are some repositories and their suppressing annotations.

 

Arrow:

Supress2

 

 

 

 

Supress3

 

 

 

 

Supress4

 

 

And finally, Kotlin:

 

 

Supress5

Fler inspirerande inlägg du inte får missa