Checkstyle- Enforcing code standards in your Java projects using Checkstyle Eclipse Plugin
Checkstyle is a tool to help programmers write Java code that adheres to a coding standard. This plugin is ideal for projects that want to enforce a coding standard.
Checkstyle does this by inspecting your Java source code and pointing out items that deviate from a defined set of coding rules. Apart from using the Eclipse Checkstyle plug-in you can also use Checkstyle from the command line or as part of an Ant build.
What’s a coding standard:
It can be any convention that you want to enforce e.g All classes names should start with a capital letter.
A rule like ‘All local variables that never get their values changed, must be declared as final’.
Good coding practices like ‘Duplicate code should not be allowed’, ‘a block should not be empty’.
Class design standards like ‘Only static final members may be public; other class members must be private’.
and many more rules and conventions. You will find a wide range of available checks here.
Follow the simple steps to use Checkstyle:
STEP 1: Install the Checkstyle plugin in your Eclipse IDE using-
URL: https://eclipse-cs.sf.net/update/ and NAME: Checkstyle update site
(if you do not know how to install plugin in an eclipse IDE, click on install Checkstyle Eclipse plugin )
STEP 2: Configurations
Sun Microsystems (now Oracle) uses some conventions and rules for developing Java; same rules and conventions come along with this plugin to your Eclipse IDE, which means that you can write Java code by following code standards followed by Sun Microsystems. You can always turn off certain checks from this default check configuration or even you can add your own new check.
After plugin installation is successful, just restart the IDE (which is needed to activate the settings). Go to window-> Preferences and click on checkstyle as in this screenshot.
If you want to use default check configuration, click on any of the entries as shown above and set it as default.
But, if you want to turn off certain checks or add your own one, follow the steps below:
1. Make a copy of the checks.
2. Select your new configuration and press Configure.
3. Activate or De-activate the checks.
4. Make this new setting your default one.
You are done. Go to project Explorer, right click on your Java Project, select Checkstyle and click on ‘check code with checkstyle’. Now, in every Java file you will observe Checkstyle highlighting code snippets violating code standards. Enjoy 🙂
I recommend using Checkstyle in your Java project. This will not only adhere your code with a set of standards but also increase your productivity by making the code much more readable and simpler.