Chapter 21. Detecting and Enforcing Coding Standards with Checkstyle[18]

Using Checkstyle to Enforce Coding Standards

Checkstyle is an open source tool that enforces coding conventions and best practice rules for Java code. Although it was originally designed to enforce coding standards, it now lets you verify coding best practices as well, in much the same way as PMD (Chapter 22) and FindBugs (Chapter 23). It works by analyzing Java source code and reporting any breach of standards. It can be integrated into your favorite IDE via a plug-in so that developers can immediately see and correct any breaches of the official standards. It can also be used to generate project-wide reports that summarize the breaches found.

Checkstyle comes “out-of-the-box” with the standard Sun conventions, including more than 120 rules and standards, dealing with issues that range from code formatting and naming conventions to Enterprise JavaBean (EJB) best practices and code complexity metrics. Checkstyle supports standards related to the following:

  • Javadoc comments

  • Naming conventions

  • File headers

  • Import statements

  • Whitespace

  • Modifiers

  • Blocks

  • Coding problems

  • Class design

  • J2EE (Java 2 Platform, Enterprise Edition)

  • And other miscellaneous issues

You can run Checkstyle from the command line, if you are so inclined. Download the Checkstyle distribution from the web site[*] and extract it in a convenient place. Then run the checkstyle-all-4.3.jar file as shown here:

$ java -jar checkstyle-all-4.3.jar -c sun_checks.xml -r ...

Get Java Power Tools now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.