Code Metrics

Code metrics can be produced by static code analysis tools to determine complexity and non-standard practices.

Why are code metrics important?

Code metrics allow developers to find problematic codebase areas that may need refactoring. In addition, some metrics such as technical debt assist developers in communicating to non-technical audiences why issues with a system are occurring.

Open source code metrics projects

  • Radon is a tool for obtaining raw metrics on line counts, Cyclomatic Complexity, Halstead metrics and maintainability metrics.

  • Pylint contains checkers for PEP8 code style compliance, design, exceptions and many other source code analysis tools.

  • PyFlakes parses source files for errors and reports on them.

  • Pyntch is a static code analyzer that attempts to detect runtime errors. It does not perform code style checking.

  • Prospector inspects Python source code files to give data on type and location of classes, methods and other related source information.

  • Flake8 is a code format style guideline enforcer. Its goal is not to gather metrics but ensure a consistent style in all of your Python programs for maximum readability. The rules for Flask8 are all defined in this list, which rolls up the Flake8 dependencies of pycodestyle, pyflakes and McCabe.

  • Black is a Python code formatter with strong, uncompromising assumptions about how your code must be formatted.

  • pylintdb puts pylint results into a SQLite database for programmatic access and searching. Ned Batchelder coded it and wrote about how he uses the program in this bite-sized command line tools: pylintdb post.

  • Flask8-eradicate (source code) is a Flask8 plugin for identifying dead code.

Hosted code metrics services

The following tools are ready to use by going to the service, punching in the URL to your site, letting them perform their analysis and then reading the results.

  • Coveralls shows code coverage from test suites and other metrics to help developers improve the quality of their code.

  • webhint, formerly Sonarwhal scans your website for accessibility, speed and security. There is both an online version that you can point at an arbitrary URL as well as a command-line version for running yourself.

  • Codecov hooks into GitHub, BitBucket or GitLab and reports code coverage on your code repositories.

Code metrics resources

Code metrics are really useful when you have a team working on a project for awhile and want to keep the code quality from degrading. However, you can easily go overboard instrumenting everything and overanalyzing the results. The following resources will introduce code metrics topics to you as well as give perspective when metrics are useful to the point of diminishing returns.

What's next after obtaining code metrics?

I've built a Python web app, now how do I deploy it?

Can I automate testing and deployments for my app?

I want to learn more about app users via web analytics.

Sign up for a monthly email with Full Stack Python tutorials. No spam ever.

Sponsored By

Okta logo.

Secure and manage identities in your Python web apps with Okta.


Rollbar logo

Fix errors in your Python code before your users see them by monitoring with Rollbar.


Digital Ocean logo.

Easily deploy your Python web apps to Digital Ocean's reliable cloud computing platform.


Scout logo

Scout monitors the performance of your Python apps, identifying slow queries, memory bloat, and more. Free during Tech Preview.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via newsletter, Twitter & Facebook.

Matt Makai 2012-2018