Linting
In short
Linting is the automated analysis of source code, without running it, to flag likely bugs, style problems, and suspicious patterns before the code ships.
What is linting?
Linting means running a tool called a linter over your source code to find problems without executing the program. A linter reports issues such as unused variables, unreachable code, a missing await, or comparisons that are almost always mistakes. The name comes from lint, a Unix tool from 1978 that checked C code for suspicious constructs, named after the bits of fluff that collect on clothes.
A linter parses the code into a structured representation and checks it against a set of rules. Each rule can be turned on or off and set to warn or fail, usually in a configuration file committed to the repository. Many rules can fix problems automatically, and linters typically run in the editor as you type, in pre-commit hooks, and in the CI/CD pipeline.
Linting is like a spelling and grammar checker for code: it won't tell you whether your argument is right, but it catches many mechanical mistakes. Linters exist for nearly every language, and teams use them to catch bugs early and keep a consistent style that makes code review faster.
Linting is often confused with formatting and with testing. A formatter only rewrites layout, such as indentation and line breaks, while a linter also looks for questionable logic. And unlike tests, a linter never runs your code, so it can't prove that the program produces the right results.
Key takeaways
- A linter analyzes code without running it.
- It flags likely bugs, risky patterns, and style inconsistencies.
- Rules are configurable, and many issues can be fixed automatically.
- Linters run in editors, pre-commit hooks, and CI/CD pipelines.
- Linting complements tests; it doesn't replace them.
Example
function getTotal(items) {
const discount = 0.1; // warning: 'discount' is assigned but never used
let total = 0;
for (const item of items) {
if (item.price = 0) continue; // error: assignment inside a condition
total += item.price;
}
return total;
}Readers ask
What is the difference between a linter and a formatter?
A formatter only changes how code looks, such as spacing, indentation, and line length. A linter looks for likely bugs and bad practices, like unused variables or accidental assignments, although some linters can also enforce style.
Is linting the same as static analysis?
Linting is a lightweight form of static analysis, which means examining code without running it. Broader static analysis tools can go further, for example by tracking data across files to find security vulnerabilities.
See also
- Code ReviewVersion Control, p. 4A code review is the practice of having other developers check code changes before they are merged, to catch bugs, improve quality, and share knowledge.
- CI/CDDevOps & Cloud, p. 9CI/CD is a set of automated practices that build, test, and release code changes frequently, so software can be delivered to users quickly and safely.
- Unit TestTesting & Quality, p. 35A unit test is a small, automated check that verifies one function, method, or class behaves correctly in isolation from the rest of the program.
- CompilerProgramming Fundamentals, p. 11A compiler is a program that translates source code written in a programming language into a lower-level form, such as machine code, that a computer can run.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
Spotted a mistake or something missing on this page?Suggest an edit