Top 5 Java Linters

yayabobi - Jul 11 '21 - - Dev Community

If you want to ensure code maintainability over the long term, you should follow best coding practices and style guide rules. One of the best ways to achieve this, while also potentially finding bugs and other issues with your code, is to use a linter.

Linters are best described as static code analyzers because they check your code before it even runs. They can work inside your IDE, run as part of your build process, or be inserted into your workflow anywhere in between.

Ultimately, it’s best to choose a linter that works best for your specific business use case and workflow.

1. Checkstyle

Alt Text
Checkstyle is one of the most popular linters available. With this popularity comes regular updates, thorough documentation, and ample community support. Checkstyle works natively with Ant and CLI. It is also available as a plugin for a wide variety of IDE’s and toolsets, including Eclipse, Codacy, Maven, and Gradle – although these plugins are managed by third parties, so there’s no guarantee of long-term support.

Checkstyle comes with pre-made config files that support both Sun Code Conventions and Google Java Style, but because these files are XML, they are highly configurable to support your workflow and production needs.

It is also worth mentioning that a project with Checkstyle built into its build process will fail to build even if minor errors are present. This might be a problem if you’re only looking to catch larger errors and don’t have the resources to fix tiny errors that don’t have a perceptible impact.

2. Lightrun

Alt Text
The second member of this list is not actually a linter per se, but it will help you improve your code quality and prevent bugs before they become serious problems. Whereas everything to this point has been a static code analyzer, Lightrun is a runtime debugger. At the end of the day, static code analysis and linting can only get you so far, so if you need a little more, Lightrun is worth adding to your workflow.

Production is the ultimate stress test for any codebase, especially in the age of cloud computing. Lightrun allows you to insert logs, metrics, and snapshots into your code, even at runtime, directly from your IDE or CLI. Lightrun lets you debug issues in production and run data analysis on your code without slowing down or interrupting your application.

3. PMD

Alt Text

What do the PMD initials stand for? It seems even the developers don’t know…

Like Checkstyle, PMD is a popular static code analyzer with an emphasis on Java. Unlike Checkstyle, PMD supports multi-language analysis, including JavaScript, Salesforce.com, Apex, and Visualforce. This could be helpful if you’d like to use a single linter for a frontend and backend codebase.

In the developers’ own words, “[PMD] finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth.” In addition, it comes with a copy-paste-detector (CPD) to find duplicated code in a myriad of languages, so it is easier to find code that could be refactored.

  1. Uncrustify

Alt Text
Uncrustify diverges from the previous linters in that Java is not its primary focus. Instead, it is a “code beautifier” made for C and C-like languages, including Java. On the one hand, Uncrustify is great for projects with a C-based or C-analogue-based workflow. On the other hand, its feature list begins and ends with simply making your code look nicer.

Uncrustify works by running through your code and automatically updating its white space, bracketing, and other formatting conventions to match a ruleset. Because this is an automated process, the developers themselves caution against running Uncrustify on an entire project without checking the changes afterward.

Uncrustify is best used in conjunction with other linters and dev tools. It isn’t particularly powerful on its own but could come in handy for niche workflows that involve multiple C-based languages.

5. Error Prone

Alt Text
Error Prone is an error finder for your code builds, specifically built for Java. It is designed to supplement your compiler’s static type checker, and find potential runtime errors without running the code. The example provided on their website seems trivial, especially to any developers who’ve been working out of an IDE for most of their careers.

But for codebases where the compile and run process can stretch into hours or even days, having that extra check can save a lot of time and headaches, especially if a particular bug might be in an uncommonly accessed block of code.

Final Thoughts

Linters make an awesome addition to just about any Java development environment. Debugging, error-checking, and issue prevention are all multi-step procedures that should take place in every phase of development. A linter is a great tool for when you want to analyze code without having to run it, maintain coding best practices, and ensure long-term code maintainability. However, each linter’s usefulness only extends so far and should thus be supplemented by other tools, including runtime debugging software like Lightrun.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .