Linters are your friends

We have all experienced this. You spend hours writing your code & every now and then tiny little squiggles pop up underneath your code. There appearance can mean only one thing , “Something is wrong and it needs to be fixed”. The program that is responsible for making this happen is called The Linter.

For those who don’t know, linters are basically your Code Guards. They ensure that your code complies to certain standards and notify you about places where there are possible errors. It also makes code reviews much much easier! Most language specific IDEs usually come with a linter installed. And in other cases, the installation is pretty straight-forward. Linters are very helpful but also frustrating at times.

The Benefits of a Linter

1. Points out well known errors

This is the most obvious benefit. All linters have a preconfigured set of rules that they check for based on the Language. For example, all the Python Linters such as Pylint, PyCodeStyle, flake8 check for PEP8 standards by default. These standards are not just focused on styling but also frequently made errors. These could be as simple as undeclared variables to checking inheritances and abstract methods and more.

2. Linters are customizable

This is were things get interesting. All linters have a configuration file. Linters may not always work properly work exactly as you want out-of-the-box. Linters have to be told what to look for and this can be frustrating when starting out. But it is easy to get used to the config file and once you are familiar enough, you can create powerful configurations that enforce standards throughout your codebase.

3. Linters are extendable

This is the most exciting part! Sometimes even after playing around with all the provided configs you may not be happy. Maybe the feature you are looking for or the error you want detected is not standard. It is a specific rule set by your company or you just don’t like an implementation. This is also slightly frustrating. But, Linters allow you to write plugins. They might have a different name based on the linter but, they are essentially that. Here you can define the rule that you want to be enforced (It is not that complicated). This is really helpful as you will see below.

In Real life

This is a common situation where linters really benefit you. This is slightly exaggerated and not exactly how it would happen, but it can happen. Let’s say you have a large Python Codebase, maybe 20,000 lines. Why Python? It is easy to show an example, but it should be possible in all languages. You are responsible for reviewing the code before it is deployed. Now we know it’s a difficult one man task. You start anyway. A few lines through the code, you see this:

eval(strange_variable)

Evals are powerful but if you don’t know what you are doing, it can quickly become a risk. So, you flag it. You keep going and then you see it again. Now you know repeatedly flagging is going to be tedious. Your team already has a linter (The team didn’t leave you completely in the dark) and for some reason it doesn’t support eval detections (Almost all Python Linters do have this) but you can write Plugins! You spend a few minutes going through the documentation and quickly write a simple custom plugin that automatically flags all evals. Yes! Job done and time saved!

But, there is a catch

You probably realised it by now. Linters are only as smart as the plugins/configs they have. As I already mentioned, getting it to work the way you want will take time. If you are just starting out, you would have to rely a lot on the docs and the quality depends. Sometimes you have great documentation but other times you don’t. In the latter case, you would have to spend hours not minutes to write your plugin or even customize the defaults. But, the good news is that once you get started, it can make your life so much easier and if you use linters right from the beginning of the development phase, code quality will always be kept in check which will in turn improve productivity!

Anyway enough talk. In the upcoming blog posts I will get into details of how a linter works and also show you step-by-step how to write a custom plugin!


Subscribe to the Blog!

Want to get notified about the latest updates?

Subscribe Via RSS  rss Subscribe Via Email