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.

获取代码度量后的下一步是什么?

我已经创建了一个 Python Web 应用,那我该如何部署呢?

我可以自动为我的应用程序进行测试和部署吗?

我想通过网络分析了解有关应用用户的更多信息。

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

Sponsored By

Rollbar logo

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

Learn More Python

Introduction to Ansible video course logo.

Deploy web apps with the Ansible configuration management tool.


TestDriven logo

Build microservices with Docker, Flask & React in this great course.

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.