What is a linter?
According to google, the definition of “lint” is “short, fine fibres which separate from the surface of cloth or yarn during processing.”
However for us programmers a linter is a software that checks your code for errors, bad practices, bad formatting and logical issues, and gives you a warning when you make a mistake. A linter can be configurable according to rules that you decide.
Why use a linter?
Well, there are quite a few reasons:
- to write better code
- to write good code more easily
- to learn a programming language
- to write code aesthetically consistent
- to follow conventions
- to share code within a team without losing your mind
Let me double stress the fact that linters are especially important if you work in a team (meaning not alone), because by following some rules you can make sure that your code looks the same regardless of who’s writing the code.
How does a linter look like?
The error position is also specified within parenthesis, in this case it is 7,12 (line 7, char 12).
While a sass linter looks like this (screenshot courtesy of Stack Overflow)
How to use a linter?
- Install Node on your machine if you haven’t already.
- Install the linters globally using the node package manager (aka NPM)
npm install -g eslint
- to install Sass lint:
npm install -g sass-lint
- Install the linter plugin in your editing software
- Create a linting configuration file for each language you need to lint
- Restart your code editor just to be safe.
Wait, what are these linting configuration files?
Linters need configuration files in order to give instructions to our linters about how we want the linter to behave. To get started, you can use some default linter configurations, and when you grow more accustomed to their usage you can change these defaults. You have to place these config files in the root of your project for it to be recognised by the code editor.
How to create a linter configuration file?
Go to your project folder using your Terminal, and type:
This will start a little program that will ask you a few questions. The first question you get is the following, just press enter to proceed.
Then you’ll be asked a bunch of additional question, choose whatever you want:
This will create a .eslintrc.json file which will look like this:
That’s it, you got a working ESlint configuration.
You can find a sample configuration file here on the official repository. Download the file and add it to your root.
Linters are available for any coding language, and the installation steps are similar to these ones, although almost certainly slightly different.