Checking Magik code using a linter

June 11, 2019    SonarQube static code analysis

Development of software can aided by use of a so-called [linter]( wiki/Lint_(software)). It is a tool to detect issues with source code and inform the developer during development.

A linter is tool to do static code analysis on source code. The linter analyses the code, determines problems with the code and informs the developer. Some (mostly open source) projects enforce a linter detects no issues with your changes before integrating the changes. There are many linters for languages, such as [PyLint](https://pylint.readthedocs. io/en/latest/) for Python, JSLint for JavaScript, [TSLint]( tslint/) for TypeScript, FindBugs/PMD/Check Style for Java.

Problems can include common pitfalls of software, such as using collection.size = 0 instead of collection.empty?, the latter being more efficient and clearer. Other problems can be formatting problems, where the agreement on how the code should look is violated, causing readability problems. An example of this is the maximum line length. Another group of problems can result in increased maintenance difficulty. For example, a method/function littered with unused variables, which confuses any (subsequent) developers working on the code.

As stated before, using the linter can help you detect issues before integrating the changes. This will increase the code quality and, in the long run, ease the development process. An often seen hurdle is where developers are not used to a linter yet and possibly disable it/ignore it.

The Magik linter being developed is part of the SonarQube plugin for Magik. Any problems which will be detected by the plugin will also be detected by the Magik linter. The Magik linter has the advantage that it can do analysis directly, instead of doing analysis afterwards. E.g., you can prevent a commit (to a Git or Subversion repository, for example) when the linter detects problems by using git/svn hooks. Information on the linter is available in the Github repository [StevenLooman/sonar-magik/magik-lint]( master/magik-lint).

An example of what the output of the Magik linter looks like is as follows, for my sw_http_server toy-project:

sw_http_server\demos\geojson_tile_server_demo\run.magik:1:0: File is not included in load_list. (file-not-in-load-list)
sw_http_server\demos\geojson_tile_server_demo\run.magik:13:51: Give the variable "e" a proper descriptive name. (variable-naming)
sw_http_server\demos\geojson_tile_server_demo\run.magik:17:1: Remove the trailing whitespace at line 17. (trailing-whitespace)
sw_http_server\demos\geojson_tile_server_demo\run.magik:23:163: Line is too long (163/120) (line-length)

The linter is open source and free to use. If you have any use for it, feel free to try it and send me a message if you need any help or want to provide feedback.