Lint-free Code with Coala

Julien Gotteland
BPAM Tech Blog
Published in
3 min readApr 26, 2017

Writing good and maintainable code is hard. Writing good and maintainable code as a team is even harder. We have different experiences and habits, we eventually use different tools, or different tools configurations and it’s easy to make mistakes when working when someone else code.

Even if software engineering is a recent discipline, various answers exist to mitigate resulting errors and control code quality.

One of them is static code analysis which is basically the process of running a program that will analyse code for potential errors.

Coala is such a tool that looks promising :

  • easy to setup, launch and embed in continuous integration workflow
  • supports various languages
  • not intrusive
  • free software and in active development
  • available in Docker

Main Benefits

  • avoid stupid bugs
  • common formatting across the team
  • low level checks are not done by code reviewer anymore

Setup

Coala only looks for one configuration file named .coafile, by default at the root of your project. This is a simple text file containing the various checks you want to run, group by sections. The various checks Coala can do are named bears.

Sections provide different configurations for possibly different languages or needs.

A bear can check code for potential problems, calculate metrics and even provide corrections. You can see this as a plugin. The list of supported bears is available here.

Let’s look at a Python example :

[Formatting]
bears = LineCountBear,LineLengthBear,SpaceConsistencyBear
files = **/*.py
max_line_length = 96
max_lines_per_file = 512
use_spaces = True

[Python]
bears = PEP8Bear,PyDocStyleBear,PyFlakesBear,PyLintBear
files = **/*.py
ignore = test.py
pydocstyle_ignore = D100, D101, D102, D103, D104, D105, D203, D213
pylint_disable = C0111, R0903

[PythonWithoutTest]
bears = CPDBear,VultureBear
ignore = test/*
files = **/*.py
language = python

[Security]
bears = BanditBear
files = **/*.py

You can see :

  • 4 sections with name between brackets
  • the list of bears to run for each section
  • the pattern to match files for which to run the bears
  • some other parameters specific of each bear

Run

As Coala has a lot of dependencies, the easiest way to run it is to use Docker :

$ docker run -ti -v $(pwd):/mnt --workdir=/mnt coala/base coala

Depending on your code quality, the output will be more or less verbose ;-) !

Continuous Integration

Useful parameters for running Coala in your CI process :

--ci non interactive mode

--no-autoapply-warn turn off warning about patches not being auto
applicable

FAQ

What bears should I use ? Simple answer : the maximum, and failed quality checks must break your build, not generate some unused reports.

How can I ignore a bear output ? You should not but you can do it sporadically. And of course this must be approved by code review.

Useful Bears for Python

BanditBear performs security analysis

CPDBear checks for code duplication

FilenameBear checks filename convention

LineCountBear checks file length

LineLengthBear checks line length

PEP8Bear runs PEP8

PyCommentedCodeBear detects commented code

PyDocStyleBear checks docstrings

PyFlakesBear runs Flake8

PyImportSortBear imports, sort them all !

PyLintBear runs Pylint

PySafetyBear checks for dependencies vulnerabilities

PyUnusedCodeBear detects unused code

QuotesBear checks quotation style

RadonBear checks code complexity

SpaceConsistencyBear checks spacing style

VultureBear detects unused code

Useful Links

--

--