Getty Images

Tip

Understanding code linting techniques and tools

Linting is an important preliminary step in the debugging process. Learn how fixing simple errors early on makes code easier to work with and improves coding practices.

Before development teams try to tackle major bugs, it's important to get the basics under control with code linting.

Code linting is among the most basic steps that development teams should perform to ensure that their software meets quality standards. By helping to identify basic programming mistakes, stylistic oversights and other issues that reduce the quality of code, the linting process enables software developers to catch and fix problems early on.

What is code linting?

Code linting is the process of evaluating code for problems such as syntax errors or failure to adhere to the stylistic conventions that a team of developers chooses to follow.

Linting is an example of static code analysis. In this type of analysis, developers debug raw source code instead of a running application. Developers that perform code linting need access to source code.

Although linting is most often used to improve the quality of application source code, there are also linting tools available for other types of code. A few examples are markdown code written in languages such as HTML and configuration code written with tools such as Puppet.

What is the purpose of code linting?

The main purpose of code linting is to detect basic code quality issues that could cause bugs within an application. By checking for stylistic issues, code linting helps to ensure that developers can easily understand how code is supposed to work and that the coding styles employed by a team of developers are consistent. Some examples of basic mistakes that linting may catch include redundant variable declarations or a missing closing bracket for a function.

Think of code linting in similar terms -- it doesn't fix major bugs, just as pulling lint off a load of laundry doesn't fix tears or stains. Linting improves the code's appearance and readability, which is the first step in optimizing code quality.

Code linting is not intended to catch complex problems in an application. Linting doesn't check whether an application properly validates input, for example. It does not detect memory management issues that could cause a segmentation fault. Developers rely on other techniques to find and correct more complicated deficiencies in code.

The code linting process is so named because of a program called Lint that was developed in the 1970s to help correct errors in C code. The name of the tool was itself a reference to the notion of removing lint from fabric to improve its quality. Think of code linting in similar terms -- it doesn't fix major bugs, just as pulling lint off a load of laundry doesn't fix tears or stains. Linting improves the code's appearance and readability, which is the first step in optimizing code quality.

When to use code linting

Since the types of issues that code linting is intended to detect are usually simple to fix, it's common to perform code linting early in the SDLC. Typically, linting is performed soon after developers write new code and before they run more complex types of tests, such as acceptance tests, which validate whether code works in the way it's supposed to, and performance tests.

In fact, some IDE tools support real-time linting, which means they can evaluate code as developers write it to catch issues such as syntax mistakes.

Code linting tools

There are hundreds of code linting tools -- also called linters -- available. They can be categorized based on the following characteristics:

  • Which language they support. Most linting tools only work with a specific programming language, so find a tool that supports the appropriate language.
  • Which types of linting they perform. Some tools are designed for specific linting functions, such as stylistic code analysis. Others aim to be general-purpose and identify a range of defects in source code.
  • Where they run. Some linters are standalone tools that developers can run directly from the command line, while others integrate into IDEs.
  • When they run. Most linting tools are designed to work only when developers actively run the tools. However, some can lint in real time, identifying coding issues as new code appears on the screen. To lint in real time, the tools must integrate into the IDE where developers write code.

To choose a suitable linting tool, consider the factors laid out above. If development teams like the idea of linting right from the IDE, for example, they will benefit from a linter that integrates with their IDE. If teams want to separate linting from other code development processes, a standalone tool would be the more appropriate option.

There are hundreds of linting tools out there. The following is a list of some of the most popular linters for widely used programming languages:

  • Pylint, a source code analysis tool for Python code.
  • Cppcheck, which can perform linting on both C and C++ code.
  • Golint, a popular linter for code written in Golang.
  • Clippy, a linter for Rust code.
  • Checkstyle, a widely used Java code linting tool.

Code linting best practices

Code linting can significantly improve code quality and consistency. To get the most value from linting, developers should consider practices such as the following:

  • Automate linting as part of the SDLC. Rather than linting on an ad hoc basis, automate the process, and integrate it into the software development workflow so that it occurs regularly and systematically.
  • Use linting to improve coding practices. Identify the types of code problems that the linting process reveals repeatedly, and then take proactive steps to avoid them. The goal is to use linting to improve development practices, not just to fix repeated mistakes.
  • Perform multiple types of linting. There are multiple types of issues that linters can detect. Ideally, make the linting process as broad as possible by checking for stylistic issues, code consistency issues, potential bugs and any other deficiencies that linters can surface.
  • Lint all types of code. Although linting is most commonly used to improve code written in programming languages, use linting to check for issues in markdown code, configuration code and most other types of code. The more types of code covered, the more value organizations can derive from linting.

Chris Tozzi, senior editor of content and a DevOps analyst at Fixate IO, has worked as a journalist and Linux systems administrator with particular interest in open source Agile infrastructure and networking.

Dig Deeper on Software development lifecycle