1

Watch our latest Product Showcase

Group 370
2

Meet us at WeAreDevelopers World Congress in Berlin

Group 370
3

Spotlight Whitepaper by IDC on Importance of Automated Code Review Technologies

Group 370

What Are Linters? (+ Why Your Team Should Use Them)

In this article:
Subscribe to our blog:

A linter. That’s the thing you roll on your clothes to get rid of cat hair, right? No, that’s a “lint roller” or “lint remover,” but the confusion is understandable because the terms are very much related. 

Introduced by computer scientist Stephen C. Johnson in 1978, the original tool analyzed source code to catch errors within the code, much like a clothes dryer’s lint trap catches lint—the tiny bits of fluffy fiber shed by clothes during the drying process. 

Linters are very important to static code analysis—the process of performing code analysis in the early stages of software development to catch and fix mistakes before executing the code. 

These tools help automate static code analysis, saving developers time through automation and helping to make sure that code defects don’t slip through the cracks and cause problems in later development cycles, where they become increasingly more difficult and time-consuming to fix. 

What is a Linter?

A linter is the most common and widely used of all static code analysis tools. Linters help developers analyze their source code to uncover potential issues, like coding errors, stylistic inconsistencies, bugs, violations of coding standards, and potential security vulnerabilities.

And while Johnson’s original tool only checked C code, you can find a linter today for just about any programming language. Likewise, you can find modern linters today that detect a much more comprehensive range of code issues and defects than their predecessors.

Early linters were created to optimize the compiling process—​​transforming human-readable source code into computer-executable machine code. Today, linters are probably even more valuable for analyzing interpreted (or dynamic) languages (like Ruby, Python, and Javascript) that don’t have a compiler to detect errors during development. 

How Does a Linter Work?

Like most static code analysis tools, a linter compares the code you’ve created with a predefined set of coding rules. If your code deviates from these rules in any way, the linter notifies you of these issues. 

Linters often integrate with other developer tools, like text editors and integrated development environments (IDEs), to streamline development workflows. 

The rules your code is being checked against depend on the linter you’re using and can often be customized and tailored to fit the specifications of your project.  

The linter first parses the source code, breaking it down into individual tokens, like keywords and operators. These basic elements are then used to build an Abstract Syntax Tree (AST)—-an abstract model of the code structure that highlights connections between various parts of the code and the hierarchies that exist among its elements.  

The linter can now analyze the source code against the defined rules, standards, and guidelines. Suppose the tool identifies rule violations and any other suspected coding issues. In that case, it will report them, providing details that help you find and fix the issue, like where it’s located (line number) and the nature of the issue (what rule was violated).

Some linters will even offer recommendations on how to fix detected issues. 

What Do Linters Check For?

While the original linters helped optimize the work of compilers, today, many linters provide a myriad of checks to help you maintain code quality. Some of the most common ones include: 

  • Syntax errors: When working with interpreted languages like JavaScript, linters enable you to verify syntax errors early in the development cycle. Teams often use pre-commit hooks with linters to prevent developers from pushing their code if syntax errors are detected. 

  • Coding standards: Linters help enforce the coding rules and conventions that are important to your team, providing code style consistency. Consistent style makes your code easier to understand. Some linters come with predefined rules, while others are highly flexible and enable you to customize what coding standards and styles your code should adhere to.

  • Code smells: Linters uncover signs that point to potential problems with your code. When you’re able to automate the process of detecting code smells, you can focus your attention on investigating them, finding the issue, and providing a resolution for it. 

  • Security issues: Data breaches resulting from undetected security problems can be catastrophic for software companies financially and in terms of reputation. Linters can help verify security rules and identify possible issues by performing security checks to detect things like secrets and insecure dependencies in your code. 

The Benefits of Linting 

Linters help you automate and simplify the process of maintaining code quality, providing the following potential benefits and advantages to development teams: 

  • Decreasing the number of errors that make it to production. When you’re able to diagnose and remedy code issues early in the development cycle, the chances of deploying problematic code are reduced considerably.

  • Creating consistent, readable, and maintainable code. Linters help to implement the coding rules and standards that provide the pillars and coding principles that lead to cleaner code that’s easier to work with and build upon. 


  • Measuring code quality objectively. When there’s complete transparency regarding what code rules and standards are essential to your team, determining the quality of code produced is no longer a subjective process. Linters provide the criteria needed for measurable code assessment.

  • Improving security. Linters can be set up to uphold specific security standards related to your industry and other regulatory requirements that are important to your organization, like OWASP Top 10. When your team can catch and fix security issues on time, you’re decreasing the chances of your code being infiltrated by malicious attacks like SQL injection, cross-site scripting, insecure authentication, XML external entities, buffer overflows, and more. 

  • Educating developers. Linters can be used to help less-experienced members of your team learn and understand coding best practices. Explaining concerns and issues in code helps developers grasp specific language complexities and nuances.

  • Saving money. Linters are essential to the “shift-left” movement in software development, which preaches code quality analysis in the early creation stages. When issues and errors are caught early, teams reduce the impact of errors and time needed to debug later. Linters that catch security issues also allow your team to avoid the potentially astronomic financial pitfalls that can result from data breaches. 

  • Enhancing team collaboration. Deciding on what linters to use and what types of issues to check for allows your team to define code quality expectations. When code quality expectations and objectives are documented, communicated, and fully transparent, collaboration throughout the entire development team becomes easier. 

Potential Pitfalls of Using Linters

Since catching coding errors early with static code analysis is a generally accepted development best practice, finding an argument against linting is pretty difficult. 

Some developers don’t like using these types of tools because they believe that receiving warnings and seeing issues popping up while they are deep in the process of writing code can stymie their flow and break their concentration. 

Constant notifications can also result in warning fatigue, which could turn into a habit of ignoring what the linter is telling you entirely. 

Some developers also worry about false negatives—code segments being flagged as problematic when they really aren’t. This can lead to developers wasting time chasing down and evaluating problems that don’t actually exist.  

As already mentioned, linting tools are better suited for developers using interpreted languages. If you are coding in a compiled language, linters might be less helpful since you’re already relying on a compiler to spot potential issues.

Popular Linters to Consider

Choosing the linter or linters to introduce to your static code analysis process depends on what programming languages you’re using and what type of checks you want to run. Let’s take a look at some popular linters to consider:

  • Linters for Security 

    • LGTM for several languages (C, C#, C++, Go, JavaScript, Java, Python, Typescript)
    • Go: Gosec 
    • PythonBandit
    • Ruby: Brakeman

Analysis Tools is a great resource for researching and finding the perfect linters for your company that lists hundreds of tools and allows you to filter them by programming language, license, pricing, and more. 

Adding Linters to Your Workflow Is Easier Than Ever

In a well-established software development workflow, it's customary to leverage multiple linters to guarantee a thorough examination of potential issues within the codebase. 

Codacy runs more than 50 different linters, helping you identify code defects, security vulnerabilities, code duplication, complexity concerns, and other issues through static code analysis. 

To get started with Codacy, sign up for a free trial today or book a demo with one of our code quality experts to learn more about how our advanced static analysis tools can improve your development process.

 

 

RELATED
BLOG POSTS

A Guide to Popular Python Static Analysis Tools
Python, known for its readability and versatility, has become a programming language of choice for many developers. As projects grow in complexity,...
Why our customers made the switch to Codacy
Software engineering teams across several industries are always looking for the best tools to use on their CI/CD pipelines. Here are the top 5 reasons...
Development workflow: 6 ways to include code reviews
The development workflow should include code reviews particularly as code reviews continue to cement as the foundation by which developers share code...

Automate code
reviews on your commits and pull request

Group 13