Introduction to checkstyle

Chanaka Rathnakumara
5 min readJun 15, 2021

What is Checkstyle?

Checkstyle is an open-source tool that helps programmers to write java codes that adhere to a coding standard. Simply, Checkstyle helps developers to check their code against a specified standard ruleset. These rules are highly configurable. Checkstyle compares written code with the specified rule set and if any violation found, it will indicate the violation. This helps to automate the process of checking common standards for a written Java code.

Why we need such a rule set?

So, why we need these standards? It is a best practice if we can follow the coding standard in software projects. It will help developers to write industry-level clean codes in more a specified way. Developers are also human beings. There will be some mistakes happening while coding. When working on large-scale projects, many developers are working on the same project. We cannot keep track of every single developer whether they are following the specific coding standards or not. To avoid this kind of scenario we need to set some common coding standards for the project. To achieve this, we can use checkstyle.

With checkstyle, we can configure necessary coding standards to our codebase. If you wonder what these coding standards are, standards like codebase should not have commented lines, command line prints, un-used imports, etc. Checkstyle supports Sun Code Conventions and Google Java Style. By default, Checkstyle uses Sun checks. Checkstyle is not bound to these 2 checks. But we can configure our own standard, as necessary.

Features

Checkstyle can check many aspects of your source code. It can find class design problems and method design problems. It also can check code layouts and formatting issues. By default, Checkstyle standards are applied on these components. But we can configure these rules for,

Annotations, Block Checks, Class Design, Coding, Headers, Imports, Javadoc Comments, Metrics, Miscellaneous, Modifiers, Naming Conventions, Regexp, Size Violations, Whitespace

Checkstyle with IntelliJ IDEA

As the first approach, I will add the checkstyle plugin to my IDE. Here I’m using IntelliJ IDEA as my IDE. First, you need to add the Checkstyle plugin from the IDEA marketplace. (“File > Settings > Plugins”)

After you install the Checkstyle plugin you can configure it as you want. You select the default configuration rule set as you wish. Or you can add your own Checkstyle configuration file to this plugin.

For now, I’m not going to select any configuration. Now when we go to the project we can check our code with a selected ruleset. As you can see below the image we can scan our project/ specific file with the selected standard.

Remember this approach is only for the IDE. If another developer comes to the project, he has to set up these standards from scratch. But what if we need to implement these standards at the code level. Then it will be easy for all the developers to follow the same coding standards for the entire project.

Checkstyle using the build file

Here I’m using maven as my build tool. Then we need to add checkstyle plugin to project “pom.xml”. The default ruleset will be Sun Code Conventions. But we can configure as per the requirement. We can get these checkstyle errors on an HTML page or on the console. The below code I configured to get the results on the console. This configuration block is totally optional. The purpose of this configuration tag is to configure the checkstyle as per the requirement.

My requirements are,

· Should use my own standards/ rule set for the code base.

· If any unused imports or imports contain star imports that should be a violation

· Code should not compile, if there is any rule set violation.

· Output should display on console.

For the above requirements, I modified my checkstyle plugin as below.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<skip>false</skip>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<failOnViolation>true</failOnViolation>
</configuration>
</plugin>

Tag Definitions

· configLocation — File that define custom set of rules

· skip — We can skip violations on the code base. We can set it as “true/false.”

· consoleOutput — Display output results on console or not

· failsOnError / failsOnViolation — Won’t allow to compile if any violation/error happens.

Then the next task is to define the custom ruleset on my checkstyle.xml file. So first we need to add the “checkstyle.xml” file to our project. Then add the below lines to the file.

<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="AvoidStarImport"/>
<module name="UnusedImports"/>
</module>
</module>

As you can see, I’ve added two modules here. One is to avoid star imports and the other one is to avoid unused imports. Then when we are going to package our code, this checkstyle specification checks my code base and if any violation happens it will give a checkstyle error. Until we fix the errors it will not allow us to compile the code.

I know you wonder, how I use these AvoidStarImport and UnusedImports modules in my specifications. So, in checkstyle it has a really good guide to write our own specification. You can use this guide to write your own specification. This is very helpful since this guide consists of examples. We can configure rules as necessary.

I’m not going to dive deep on these tags since this is just an introduction. If you want to know more about these tags, refer to this article. It explains clearly what and why we use these tags

I hope you enjoyed this article. Happy Learning!!!

--

--

Chanaka Rathnakumara

Tech Enthusiast || Full Stack Developer || Freelancer || Cloud Architect