JetBrains logo

Static Code Analysis Guide

Qodana / Static Code Analysis Guide / What is a linter?

What is a linter

If you’re asking “what are linters?”, you’re probably interested in code quality and improving product performance. Let’s take a look at what they are, how they work and why it’s so important to incorporate them into your quality assurance cycle.

Linters definition: What do linters do?

At the basic level, a linter is a tool within static code analysis that examines source code to flag mistakes, anomalous code, probable bugs, stylistic errors, and anything that deviates from the coding standards your team has agreed on. This could be as simple as a spelling error or highlighting problems like license incompatibility. It just depends on your use case.

Linters can analyze your codebase before you execute, which helps you improve your codebase in the early stages of development and beyond. Although code quality is important at every stage, this early stage in particular is where you can save yourself the most trouble down the line.

Linters meaning: examine source code for mistakes or license incompatibilities

Why do they call it a “linter” anyway?

The boring answer is that the term “linter” originates from a 1970s tool for the C language called Lint, which checked for bugs and style problems. It was created by Stephen C. Johnson, who opened the doors for linting as we know it today.

But the more figurative, unofficial explanation is that “linter” derives from “lint brush” – a tool for removing dirt, debris from clothes. Whatever the origin, what we do know is that linters help improve code quality.

Why incorporate linters and static code analysis into your dev workflow?

The simple answer is that linters help you achieve clean code. So beyond the obvious benefits of more maintainable and sustainable code, it also leads to better product performance and a range of other benefits for diligent dev teams.

This list includes everything from helping you enforce coding standards, to improving code quality, weeding out errors, and saving you endless hours of overtime and stress spent on emergency fixes.

Amit Weinblum
Infrastructure Team Leader at Moovit

“In the long term, Qodana has stabilized our production system and allowed developers to avoid fixing issues too late in the pipeline. We’re seeing the shift of the paradigm, where development teams follow the policies and save a few sleepless nights with static code analysis”

Exploring use cases for modern dev teams

Now that we’ve covered what linters are and what they do, it’s time to explore these use cases in action.

Detect anomalous code and probable bugs

The only thing worse than having a critical bug in your code is not knowing about it. And it’s not only critical bugs that cause issues for developers and users down the road. The longer they’re there, the more impact they can have – so find them faster using linters.

The same goes for probable bugs like duplicate declarations, incorrect inclusion calls, and conflicting type variable counts, which can now be easily detected before they wreak havoc.

Eliminate dead code

Cutting out redundant, complex, and unnecessary code can help you make your codebase leaner, faster, and easier to keep under control.

Highlight spelling problems

Spelling errors are easy mistakes to make, but they could lead to software giving the wrong answers, crashing, or losing data. Linters can spot issues like this before they become a problem.

reports spelling problems

Improve overall code structure

It’s easier to debug and share code that’s readable, and it’s easier to create readable code by improving the structure. This could include relatively simple things like choosing meaningful variable and function names or deciding whether they’ll be in Camel or Snake case.

It can also mean creating reader-friendly README files or avoiding unnecessary loops and iterations. Each of these measures have unique benefits that help you make sense of code more quickly.

Introduce coding best practices

Avoiding long functions and hardcoded passwords are just two examples of coding best practices that help you create more secure and better-quality code. Updating your comments, using consistent formatting, and staying on top of version control make all the difference when working as part of a team. Linters can help you enforce best practices and identify when they haven’t been adhered to.

Check third-party license compatibility

This license audit feature is available in several linters and essentially helps you make sure that your project license is compatible with the licenses of its dependencies.

Upload analysis reports

As a JetBrains user you’ll probably upload your reports to the Qodana Cloud or a similar tool. From here, you can view the findings of your linters’ analysis.

Example of a Qodana Cloud report

Now let’s dig a little deeper: Linters in action

Code linters can work in various ways, but for the sake of this guide, we’ll look at some of the most common practical applications.

  • Parsing code: This is the process of “understanding” the structure of the source code and what it means. One method of achieving this is through lexical analysis.

In this case, the source code will get broken down into tokens like keywords, identifiers, operators, literals, and even punctuation (in other words, the smallest parts). From there, the linter can read the code and create an abstract syntax tree (AST) – this basically shows which data is most important in the source code.

  • Applying rules: The linter crosschecks this syntax tree in relation to the ruleset. Sometimes the rules can be customized, as is the case with Qodana.
  • Reporting issues: The linter’s going to pick up on deviations here and generate a report that shows you what the issues are as well as where they are located in the code and, in a best-case scenario, what corrections you could make and why.

It’s also worth noting that linters can be integrated into text editors and CI/CD pipelines (as is the case with Qodana) and all sorts of developer environments, which means you can get real-time feedback on flagged issues and what needs to change.

Local inspections and licenses report

Linters as a Qodana user

If you’re interested in code quality and improving productivity in your team, JetBrains Qodana has you covered. Qodana linters offer you a variety of inspections for several programming languages. It’s also a useful tool for setting up quality gates in your CI/CD pipeline and collaborating on creating better-performing products.

Catch Issues Early With Qodana Linters

Improve code quality and prevent bugs before they reach production — integrate Qodana’s powerful linters into your development workflow today.