📗

Checkstyle

2023/11/11に公開

What is Checkstyle

Checkstyle is one of the static code analysis tools designed for the Java programming language. It automatically inspects source code according to rules regarding the quality and style of the program, ensuring compliance with coding conventions and best practices. It serves to enhance code quality and maintain consistency among developers. The following outlines the process of integrating it into a project.

Installation

I usually use Gradle in my projects, so I install Checkstyle using Gradle. Alternatively, it can be installed directly from the website. Add the following code to the build.gradle file. (As of November 2023, version 10 is available.)

To output reports and view them on the screen, add the following:

Configuration File and Execution

Prepare the configuration file checkstyle.xml under config/checkstyle by downloading the base configuration file google_checks.xml and renaming it to checkstyle.xml. This file is commonly used to maintain consistent coding styles when developing with multiple team members. Run Checkstyle with the gradlew check or gradlew build command, and an HTML file will be created under build/reports/checkstyle/. View the results in a browser, as demonstrated in the following example page:



This example is from reference(Baeldung, 2022)

Conclusion

Checkstyle is a source code static analysis tool that simplifies the standardization of coding styles, especially useful in collaborative development. The configuration is straightforward, making it very convenient. Running it during development outputs reports, highlighting violations of coding styles for review and correction after coding.

Discussion