Programming Tools: Code Complexity Metrics

In this month's column, the author explains how to determine code complexity with complexity metrics and introduces his own metric, PyMetric.
PyMetrics

A number of commercial tools are out there, as well as some open-source ones. None seem to give you the ability to define your own metrics, however, or to use extended versions of existing metrics. Also, of the open-source tools I found, most were outdated or no longer maintained. The one exception is the Eclipse Metrics Plugin.

For the rest of us, I decided to write an open-source program to produce metrics that end users can compute and modify. The program is written in Python and currently is limited to analyzing Python--thus the name PyMetrics--but the principles can be extended to any language. By writing the code in Python, you should be able to understand the program better than if I had written it in almost any other language. The major advantage of this approach is the output format. It lets you work with the raw numbers to produce your own reports and metrics.

The PyMetrics project is hosted on SourceForge.net, and the files can be downloaded from the project page. Please note that due to the short time I had to produce the program, it is not as polished as I would have liked. The things I would have liked to do include but are not limited to:

  • Showing what are considered industry standard metric values as a point of comparison for your metrics.

  • Simplifying some of the modules, including tokenize and the main module, PyMetrics.

  • Providing better modularization to allow others to extend this program more easily.

  • Defining a set of test suites.

  • As always, offering better documentation.

Conclusions

Metrics are numbers representing the complexity of a given program. Although some accepted standard measures exist, you need to decide what works for you and the acceptable thresholds.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

There is a more standard metric

Anonymous's picture

Function points are a more established metric than cyclomatic complexity.

Wrong

Charlie (Colorado)'s picture

You're wrong in at least two aspects:

(1) function points are a measure of *volume*, not *complexity*. Two programs can have wildly different cyclomatic complexities, but the same function point score.

(2) McCabe's complexity is at least as well established as function points, at least among those who know enough of the literature to understand the difference between a volume and a complexity measure.

Function points

Anonymous's picture

I have tried working with function points. The effort has not been successful for two main reasons. One, the definition of what consistutes a function point always seems to be in flux - making it almost a "black art". Second, most tools using function points seem to be proprietary and how they compute their function point metrics are secret. That said, there seems to be a strong community who support function points. A simple introduction for functions points can be found at http://www.ifpug.com/fpafund.htm

White Paper
Fabric-Based Computing Enables Optimized Hyperscale Data Centers

Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions