Catching common Java mistakes with Error Prone

Daan de Rond

Consultant / Developer

Last month a few colleagues and I went to J-SPRING in Utrecht. We were really intrigued by a talk about Error Prone by Rick Ossendrijver and decided to explore the possibility of using this within Kabisa. Error Prone is a powerful software analysis tool created by Google that helps developers detect and fix coding errors in Java that are not detected by the compiler. I’ll show some simple examples of how it works, how easy it is to add custom checks and what to take into account when enrolling it within your team.​

Quality and uniformity inside your codebase

I think we’ve all had PR reviews where we notice the same coding mistake / convention keeps recurring. Error Prone can help you spot and fix these issues automatically, resulting in more efficient code reviews.​Let’s take a look at a simple example:​

				
					String company = "Kabisa";
String message = "Would you like to join " + company + "?";
​
System.out.println(message);
				
			

While this code works correctly, it can be improved for readability and performance. Error Prone provides a check called ‘StringConcat’ that can suggest using ‘StringBuilder’ or ‘String.format()’ instead of concatenation with the ‘+’ operator.​Here’s an updated version of the code that follows the recommended convention:​

				
					String company = "Kabisa";
String message = String.format("Would you like to join %s?", company)
​
System.out.println(message);
				
			

​​When running Error Prone with the ‘StringConcat’ check, it would first provide a warning or suggestion to use ‘StringBuilder’ or ‘String.format()’. Error Prone also has the ability to generate patch files containing recommended fixes, and it also offers the option to apply those changes directly to the codebase. This would be great to use in a pre-commit hook, making sure these issues don’t end up in your PR.​​

Custom checks

Ofcourse, every project / domain has its own unique requirements and coding conventions. By adding custom checks, you can enforce specific rules or patterns that are relevant to your project, domain or organization. Adding these checks is done by using Refaster templates, a before/after java refactoring tool by Google, which is bundled with Error Prone.​In the code below you can find an example of one of these custom rules.​​

				
					import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.AlsoNegation;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
​
public class StringIsEmpty {
@BeforeTemplate
boolean equalsEmptyString(String string) {
return string.equals("");
}
​
@BeforeTemplate
boolean lengthEquals0(String string) {
return string.length() == 0;
}
​
@AfterTemplate
@AlsoNegation
boolean optimizedMethod(String string) {
return string.isEmpty();
}
}
				
			

​​Where @BeforeTemplate defines the conditions that should trigger this rule and @AfterTemplate defines the correct replacement.​​

Build tool integration

Error Prone runs within your standard build process, supporting several build tools like Maven, Gradle and Ant. It’s as simple as adding the dependency and making sure it runs during your normal build process. Information about how to install it in your Java project can be found here.​

Convincing your co-workers to invest in Error Prone.

To achieve successful rollout within a team/company, effective communication plays a crucial role. When integrating Error Prone into the Java environment, it becomes imperative for developers and teams to engage in open dialogues. By involving developers from the early stages and clearly communicating the advantages of Error Prone, teams can collectively establish coding guidelines and rules that will be enforced. This collaborative approach not only helps set expectations but also ensures that developers comprehend how Error Prone fits into the development workflow, minimizing the likelihood of it being perceived as a burden.​

Comparing it to other code analysis tools

While there are several other code analysis tools available for Java, like SonarQube, Checkstyle and FindBugs, Error Prone offers some unique features and advantages.

  • Customizability; Error Prone allows you to customize and extend its set of checks. Allowing you to easily create your own rules or configure the existing ones to fit your specific needs and coding standards.
  • Patching; As mentioned before, Error Prone also offers the ability to patch the warnings/errors it has identified, automating this replacement process.
  • Build tool integration; This allows you to easily incorporate Error Prone into your build process and automate the analysis and patching of your codebase.

Conclusion

Error Prone is a powerful tool for Java developers that enhances code quality and efficiency. By seamlessly integrating into existing workflows, it automates the detection and correction of common coding mistakes. Error Prone’s customizable checks enable developers to enforce project-specific conventions, ensuring uniformity within the codebase. Effective communication and collaboration are key to successfully implementing Error Prone, as it fosters open dialogues and aligns developers with its advantages.​

Overall I think Error Prone can be a great addition to our development process, and we’re definitely going to try it out on one of our repositories!

Co-creatie begint hier

Daag ons uit!

Organisatie
E-mailadres
Naam
Telefoon (optioneel)
Bericht