Tagged with

A couple months ago, a friend asked me if I was using a linter on my code. I said yes, and after talking with other developers I just realized how many people are not using linters on their projects. The same friend said to me: “linters are not good because it causes my CI to fail.” Developers often miss the point of having a linter: to enforce reasonable style guidelines in a software development project. I told him that the linter should fail your builds when the code is written improperly. Grammar ensures that our writing is legible and consistent between authors. Just like a grammar check in a word processor, linting provides this same utility inside of your codebase.

Grammar ensures that our writing is legible and consistent between authors. Just like a grammar check in a word processor, linting provides this same utility inside of your codebase...

Linters should be run in both your editor and CI setup -- and many developers run them as a pre-commit hook in their version control. Running a linter should be a normal part of life as a developer, and while the first few days suck, you’ll quickly get productivity gains that outweigh your initial losses. It’s not hard to get started using a linter, and the practice will help your team write consistent and better code.

Another often cited reason for not using linters is that everyone on the team is a senior developer. While individual senior developers may write wonderfully consistent code, that doesn’t mean they all write code consistent with each other. Linters are about consistency, not functionality. The more consistent your code is, the easier it becomes for anyone to dive in and help, and the more effective you become working inside it.

There are a lot of linters out there, let’s take a look at some of them that are fantastic.

Coala

You can’t talk about linters without mentioning Coala. Coala is an open source project that provides a unified interface for linting. Coala works as a linter regardless of the programming language you are using. It has support for many languages. Check out Coala in your preferred language.

Prettier

I love Prettier, and use it automatically after saving a source file. We have one episode on DailyDrip where I set up prettier with vim. Prettier is an opinionated code formatter. It supports JSX, Flow, TypeScript, and others. If you don’t want to waste my time checking what rules you should use in your code, then Prettier is a great option. You can also use eslint with prettier.

Rubocop

Ruby is a great and flexible language, and while that provides a great deal of power to the developer writing it, Ruby projects can become a mess of varied syntax and spacing rules. Rubocop has helped a lot of the teams I have worked with. Not only with the basics, but it is wonderful at teaching beginners best practices. You can learn idiomatic Ruby better because of rubocop. Start by reading the Ruby Style Guide Rubocop uses.

Stylelint

We use stylelint for our CSS and SCSS files. It is a completely unopinionated linter, and while it ships with hundreds of possible rules you can use, it’s really easy to mix and match the ones you want. You can also create your own rules.

Pylint

The Python language also has an interesting linter. Pylint is something you should take a look if you write code in Python.

Elm-Format

I can’t use Elm without elm-format. Elm has a non-common indentation pattern when compared with other most used languages. For example, in the following code, the comma is at the beginning of the line. This is not common in languages like Javascript, but the for the elm community this is normal. When I am coding in Elm, using elm-format helps me follow the standard proposed by the community.

...
userLink maybeUser =
    case maybeUser of
        Just user ->
            a
                [ Route.href <| Route.User user.username
                , class "user-name"
                ]
                [ text <| User.usernameToString user.username ]
...

Elixir

Linting isn’t as mature in the Elixir world as it is in more established languages like Ruby or JavaScript. Elixir will have a built in code formatter.

One of the most famous is Credo. If you are rubyist, Credo is similar to Rubocop. It is a static code analyser. There is also an interesting project that allow you to run Credo against any GitHub repository. Another interesting project is Dogma.

For text editors

Atom

I think all good text editors have the option to enable linters. It’s normally easy to set it up. For example, Atom has the option of linters, and we can set up other linters on it.

Vim/Neovim

For vim, I am using ale to help me run the linter asynchronously.

At a glance

Your code editor probably supports a linter. Don’t wait, use these tools to help you write higher-quality code.

What is you favorite linter? Let us know in the comments, and we’ll edit it into the article so others can find it easy!

Resources


33b32be65cd662922a71dc1d051ddb9f?s=184&d=mm

Franzé Jr Software Engineer with experience working in multi-cultural teams, Franze wants to help people when he can, and he is passionate about programming and Computer Science. Founder of RemoteMeetup.com where he can meet people all over the World. When Franze is not coding, he is studying something about programming.