Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Code linting explained

Friday, March 17th, 2017 by Servage

codeLinting is a term you hear every now and then, but what does it mean? Linting is related to source code analysis, which is something you should do for your code as you develop. After reading this article you will be more familiar with these things. Let’s find out what linting is and how to do it with CSS, JavaScript and PHP code.

Linting Explained

So, what is this mysterious “linting” people are sometimes talking about? Simply put, linting is a code review method where a program automatically analyses source code to detect issues. Lint was the name given for a C program that was used for this type of code analysis decades ago, but the term still remains in use today.

Linting is not only for checking code for programmatic errors but also to check whether the code follows common standards and guidelines. The guidelines can be defined by a framework such as Symfony, application like WordPress or by language-wide standards, for example the PHP-PSR.

Let’s next have a look at how linting works with different languages. There are many different tools to lint code. Out of the tools we are using in this article, CSS and JavaScript linting tools are the easiest to use, so let’s start with them.

Linting CSS with CSS Lint

Checking your CSS code for possible improvements is indeed simple. Go to www.csslint.net and paste the styles from your style sheet into the big box. When you click “lint”, the tool will check your code with the default rule set. The tool will give you a list of recommendations based on the chosen rules and your code, such as reducing the amount of !important properties since using them is generally considered bad practice. This is a great and fast way to analyze and improve your CSS code.

Linting JavaScript with JSLint

You probably wish that checking JavaScript code would be as easy as CSS with the above tool. In fact, it is! There is a similar online tool at www.jslint.com for JavaScript. Simply copy and paste your code into the text box, tweak the rules and click the lint button.

Linting PHP with CodeSniffer

Online tools can be handy when you want to occasionally check your code for improvements. If you want to do it more often or don’t want to share your source code with online tools, it’s better to integrate linting directly with your IDE. By doing it this way, your IDE is constantly checking your code as you type, and immediately warns you about stylistic issues and other problems with your code.

Setting up CodeSniffer is a little more complicated and depends on what IDE you use, so therefore it cannot be covered entirely in this article. If you would like to give it a try, head to pear.php.net and search for CodeSniffer. Since CodeSniffer is a PEAR package, you have to install it using PEAR, a package manager similar to Composer.

Code linting explained, 5.0 out of 5 based on 2 ratings
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.