What is Linting? How does a linter work?

Improve your code with lint checks
  • Indexing beyond arrays.
  • Dereferencing null pointers.
  • (Potentially) dangerous data type combinations.
  • Unreachable code.
  • Non-portable constructs.

Where to Use Lint?

A linter is great for identifying errors when you use standard rules. Remember, a linter analyzes your code for stylistic and programming errors against the rules it knows. This can depend on your programming language. Some languages are better suited for code linting than others.

Pros & Cons

Now up to this point, you probably at least a general idea of what a linter can do for your code. But in more practical terms, what are the benefits that this type of tool can provide? Here are some:

  • Fewer errors in production. The use of linters helps to diagnose and fix technical issues — e.g. code smells — in the code. As a result, fewer defects make their way to production.
  • Readable, maintainable, and more consistent code. Linters can help teams achieve a more readable and consistent style, through the enforcement of its rules.
  • Fewer discussions about code style and aesthetic choices during code reviews. Code review discussions shouldn’t be riddled with endless arguments about stylistic preferences. Linters can take those topics out of the way.
  • Objective measurement of code quality. Discussions about code quality often veer into subjectivity. Linters provide an objective and measurable assessment of code quality.
  • More secure and performant code. Not all linters analyze source code regarding its performance and safety, but some do.
  • Education about code quality reaches more developers. Linters can help developers — particularly the most inexperienced ones — to learn about code quality.
  • The most obvious argument against it is about it breaking concentration. If you are trying to get into the flow and crank out lines of code, a bunch of flags and warnings popping up can take you out of it.
  • Another danger is developing warning fatigue. This is often the result of false negatives and making assumptions about the reliability of linters. If you get into the habit of ignoring all the flags, your linter can protect you as little as the warm, fuzzy-feeling you get inside from gazing at the ever-present check engine light.
  • Using a linter on a massive existing code base is something many would advise against as “implementing a linter on a mature project becomes a tremendous task.”

Examples of linters

There are a vast number of linters out there. Depending on the programming language, there are even more than one linters for each job.

Linters focused on the style guide and coding conventions

Linters focused on Security:

Types of Checks Linters Provide

As you’ve just read, the original lint tool analyzed code to enable optimizations for compilers, but over time, more advanced and complete tools were released. Nowadays, we have myriad different linters, which provide many types of checks. Let’s quickly go through some of them.

Syntax Errors

The most basic and more vital type of checks that a linter can provide are syntax error verifications when it comes to JavaScript and other interpreted languages. Developers shouldn’t even push code to the mainline without it passing syntax verifications.

Code Standards Adherence

Another vital type of check that linters provide is adherence to coding standards. Some people might dismiss this as a purely aesthetic concern, but they’d be wrong. Having a single consistent coding style is beneficial for decreasing the cognitive load of reading, understanding, and maintaining code. A codebase that has a consistent code style will be easier to understand, and the developers that use it will be less likely to introduce bugs.

Figure 1 Third Way https://xkcd.com/1285/

Potential Problems (a.k.a. Code Smells)

Code smells are signs that something might be wrong with your code. It’s amazingly useful to have a tool to detect those signs automatically, so you can investigate them further, if necessary.

Conclusion

Linters help you get more productive and save you time and money. They drive your team to better decisions (those oriented by data) and share ownership over the quality.

 by the author.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CetinOgut

CetinOgut

33 Followers

A Full-stack developer | Project Manager | SOF Veteran | Tech Enthusiast who is trying to be a human among people