Measuring code quality with Wily

So far, we've figured out how to keep code formatted, but is this the only factor when it comes to code quality? Of course not; in fact, there are plenty of abstract metrics to take into consideration when it comes to the quality of code, such as the following:

  • Lines of code (the fewer lines there are, the fewer bugs there will be).
  • Cyclomatic complexity, which counts the number of logical branches in the code; for each if/else loop, or another indentation block, complexity grows by one. 
  • Maintainability indexa measurement that mixes cyclomatic complexity, lines of code, and the number of variables.

But how are those metrics useful? Every task is different, and there are problems that require code that's complex and hard to maintain. Therefore, we shouldn't be too serious when it comes to comparing metrics across different code bases. With that being said, it is a great idea to track metrics over the same code base, measuring trends. Often, those metrics can highlight problematic areasor problematic new code—and spur a conversation, and, perhaps, critical rethinking.

To track our maintainability metrics over time, we'll use a wily package. First, we'll have to build a cache of all the previous commits. It will take a couple of seconds for wikiwwii to do this, but for larger datasets, it could take longer:

wily build wikiwwii

Now, we can measure the code's quality via a table, or by drawing a plot:

wily report wikiwwii cyclomatic.complexity -n 10

>>> --------History for ('cyclomatic.complexity',)------------
╒════════════╤══════════════╤════════════╤═════════════════════════╕
│ Revision │ Author │ Date │ Cyclomatic Complexity │
╞════════════╪══════════════╪════════════╪═════════════════════════╡
│ 865a172 │ Philipp Kats │ 2019-07-01 │ 5.3 (0.0) │
├────────────┼──────────────┼────────────┼─────────────────────────┤
│ 4ff4c88 │ Philipp Kats │ 2019-06-06 │ 5.3 (0.0) │
├────────────┼──────────────┼────────────┼─────────────────────────┤
│ 537bca7 │ Philipp Kats │ 2019-06-06 │ 5.3 (0.0) │
├────────────┼──────────────┼────────────┼─────────────────────────┤
│ 462c514 │ Philipp Kats │ 2019-06-06 │ 5.3 (0.0) │
├────────────┼──────────────┼────────────┼─────────────────────────┤
│ f2fee0f │ Philipp Kats │ 2019-06-06 │ 5.3 (0.0) │
├────────────┼──────────────┼────────────┼─────────────────────────┤
│ a46ff3a │ Philipp Kats │ 2019-06-06 │ 5.3 (0.0) │
├────────────┼──────────────┼────────────┼─────────────────────────┤
│ e5eae97 │ Philipp Kats │ 2019-06-06 │ 5.3 (0.0) │
├────────────┼──────────────┼────────────┼─────────────────────────┤
│ 0be3db4 │ Philipp Kats │ 2019-06-06 │ 5.3 (0.0) │
├────────────┼──────────────┼────────────┼─────────────────────────┤
│ fd36181 │ Philipp Kats │ 2019-06-06 │ 5.3 (0.0) │
├────────────┼──────────────┼────────────┼─────────────────────────┤
│ cd2ff2a │ Philipp Kats │ 2019-06-06 │ 5.3 (-1.92222) │
╘════════════╧══════════════╧════════════╧═════════════════════════╛

Since our package is very young, not many changes have to be made. We won't want to check this report every time; instead, we'd rather automate this check and ensure we're not decreasing the code's quality. For that, there are two ways to achieve that:

  • First, wily diff will show differences between the current version of the code and the previous commit; it is a good idea to check code quality throughout your work.
  • Second, we can integrate Wily as a pre-commit hook, similar to how we made it with black; we'll start by adding a corresponding config to the .pre-commit-config.yaml file, just below the black settings:
- repo: local
hooks:
- id: wily
name: wily
entry: wily diff
verbose: true
language: python
additional_dependencies: [wily]

Now, we will rerun pre-commit install. Finally, it is a good idea to add wily as a dev-dependency to poetry with poetry add wily and check the pyproject.toml file:

[tool.poetry.dev-dependencies]
pytest = "^3.0"
pytest-cov = "^2.7"
pytest-azurepipelines = "^0.6.0"
black = "^19.3"
wily = "^1.12.2"

With that, let's commit our new changes. Now, there are now two pre-commit processes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.52.8