While developing applications, it is very important to maintain good code quality and follow the coding best practices. Application with high quality code is easier to maintain in future.
SonarLint is an open source code quality analyzer IDE extension, which helps us keep a check on code quality while writing the code on IDE. Identifying the code quality issues while writing the code is way better rather fixing them later, as it may consume more time or may require a lot of changes to fix them, that could have been avoided while writing the code itself.
In this post we will see Advantages and how to use SonarLint plugin on Eclipse and how to use it to analyze quality of the code we write. Let’s first see the advantages of using code quality analyzer tool like SonarLint plugin.
Advantages of using a code quality analyzer
- Makes sure that the coding best practices are followed and code has been written the right way.
- Reduces the time taken for manual code review process as basic code quality and coding best practices are already tested.
- Increases the code quality and productivity of the developer.
- Forces the developer to make use of common coding best practice. Since SonarLint provided inline suggestions, code can be modified easily while writing on the IDE itself.
- SonarLint provides suggestions for the bug/ issues/ coding best practices, along with description and examples.
- SonarLint Rules can be disabled or enabled at any time. Plugin also can be enabled/disabled at any time completely or for particular project .
Adding SonarLint plugin to Eclipse IDE
SonarLint plugin can be added to Eclipse in two ways.
- By visiting Eclipse market place and drag and drop the “install” button to eclipse IDE.
- Second way is by navigating the following options on Eclipse tool bar. Help > Eclipse Marketplace. This will open up the market place search window as shown below. Under the search menu or search window, search for SonarLint plugin. Once the SonarLint plugin is appeared on search result, click on install.
Analyze the code using SonarLint
We have successfully added SonarLint plugin to our Eclipse IDE. Now we can analyze our code. Code can be analyzed on project level or on individual file level. To analyze project/ specific file, right click on file/project > SonarLint > Analyze.
Following is a sample analyzed result of simple Java code with issue report, description and suggestions.
Disabling SonarLint for particular file of a project
We can disable/enable SonarLint from analyzing code for particular files by right click on file to be excluded > SonarLint > Exclude. This can also be done from right click on project > Properties > Navigate to SonarLint > File Exclusions, and add the list of files to be excluded. Click on Apply button and then OK button to save the settings.
Deleting issues report on Eclipse
After analyzing the code, a issues report will be shown as shown above. this can be cleared by selecting issues to be deleted and then right click on issues report > delete. Issue report can be generated again by analyzing the file/ project again.
How to enable/disable rules on eclipse
We can choose which are the rules should be applied for analyzing the file/ project. To view list of all the available SonarLint rules, navigate to Window > Preferences > SonarLint > Rules Configuration. Here we can enable/disable the rules to be considered for analyzing the code.
We can also deactivate individual rules also by hovering on the highlighted error warning as shown below.
SonarLint is a good code analyzer tool which can be really helpful for maintaining basic quality of the code. SonarLint is also available for other IDE’s like Visual Studio, IntelliJ IDEA,etc and can be used to analyze program languages like Java, Java script, PHP, Python, etc. Take a look at official web site and try it’s cool features.
- AJAX Example using Servlet and JAVA
- Hello World Servlet using Eclipse and Tomcat
- AJAX Example using Servlet and JAVA