Sunday, May 15, 2016

Real Time Code Correction with Linters

If you are a regular around here, or if you even took a look at the date since the last post, you may have noticed a gap. As tends to happen in the blog world, I took a hiatus to focus on other projects and research. This was actually very productive and I am excited for you to see the fruits of those labors in the coming months. So thanks for sticking with us and joining in the return of Prophage activity.

This week I want to talk about improving our everyday programming using something called a linter. A linter is simply a program that runs with a text editor and checks for stylistic and programming errors (if you already know about these, I apologize as this is a simplified explanation). So put another way, it is like the spellcheck and grammar check functions we have seen in word processors (e.g. Microsoft Word) except it works with programming languages. Now I have only started using linters in the past couple of months, but I have totally fallen in love and wish I had been using them a long time ago. In this post we are going to familiarize ourselves with linters and hopefully end up downloading them to use in our own programming.

The first question we might ask is why we should use a linter? It sounds like just another complicated program to have to deal with. Well just like when we type emails or text messages, we make mistakes like typos and we rely on spell check and grammar check to alert us when a potential mistake has occurred. In the end we get a much cleaner, clearer, and professional document. A linter does this with code. It will alert us when it appears we have made a syntax error, or may have written in something unstable that could behave unexpectedly. An example of such a correction is changing working directories in a bash script.

# If I write this
cd ~/documents
# My linter tells me "Use cd ... || exit in case cd fails".
# So I change it to this safer line
cd ~/documents || exit

Not only did the linter save me from an unsafe directory change, but if I was unaware of that danger in the past, I now know and can avoid it in the future. So in the end it can save you from errors, as well as teach you along the way.

So now that we have seen linters can be helpful, how do we set them up on our computers? There is linter for almost every language, and these linters can be used in almost every text editor. I spend a lot of time programming in Bash, R, and Perl, and I have found the associated linters to be incredibly beneficial.




Shell Check


Perl Linter

Perl Critic

I use all of these in Sublime Text, but you can also use them in most other text editors. Check this out for help getting started with Sublime Linter. Please note that downloading Sublime Linter does not include all of the linters for various languages, so you have to install those additionally. However this installation is easy and they walk you through the process.

And there you have it. Everything you need to get started with linters in your own programming practices. As always, if you have any questions, comments, or concerns about this post, please leave me a message in the comments. You can also reach out to me by email or Twitter.

Happy coding!

*Image Source

No comments:

Post a Comment