Measuring Code Complexity Metrics - II


Contents

Introduction

by Anand B Pillai

In my previous article on measuring code complexity, I introduced the McCabe metric for calculating the cyclomatic complexity of your code. In that article, I had mentioned some tools which allow you to measure complexity metrics. This article talks about some of these tools, focussing on the PyMetrics tool.

Metrics tools '

A number of commercial, non-commercial and opensource metrics tools are available for measung code complexity metrics. Let us take a look at some of these tools.

Eclipse Metrics Plugin

There are a couple of projects which provide code metrics plugins for the IBM Eclipse project.

The Eclipse Metrics plugin project is the most active among them. The plugin can be installed directly from Eclipse by using the Eclipse software update service. Details are available from the project website. The eclipse metrics plugin provides measurement of a number of metrics including the cyclomatic complexity. Some of these are:

  • TLOC = Total lines of code
  • NOC = Number of classes
  • MLOC = Method lines of code
  • WMC = Weighted Methods per Class
  • VG = McCabe cyclomatic complexity

This plugin provides measurements for about 20 metrics.

Cyclo
Cyclo helps one to analyze complexity of ANSI C/C++ code. It was written by Roger Binns in 1993 in C++.

Metrics
metrics is a collection of tools for measuring various complexity metrics including cyclomatic complexity. It can also measure other indices such as Halstead, kdsi etc. It is written in C.

PMD
PMD is a tool for scanning Java source code and providing metrics on the code.

PMD contains a number of built-in rules for analyzing Java source code and report anomalies such as unused code, empty blocks etc. One can also extend PMD by writing rules to look for specific problems in the code. PMD also allows to obtain metrics on cyclomatic complexity and code coupling, apart from many others.

For more on PMD, read this article.

PyMetrics

PyMetrics is an opensource code complexity metrics measurement tool written by Reg Charney. It works only on Python source code.


PyMetrics can be downloaded as a tarball from its SF project page. There is no distutils installation required for this tool, it runs out-of-the-box.

Using PyMetrics
Using PyMetrics to calculate code complexity of your Python code is straightforward.

The interface to the tool is the module named PyMetrics.py. The module is executable. Running the module with no arguments, prints a lot of help information on how to use the tool.

PyMetrics is very customizable. The standard way to work with it is to execute the PyMetrics.py module with the Python source file you want to analyze as argument. For example,

$PyMetrics.py mymodule.py

PyMetrics will perform a static analysis of your code and print out information such as,

  • Reports extra exit conditionals
  • Block count
  • Maximum block depth
  • Number of doc strings for Python classes
  • Number of classes
  • Number of comments
  • Number of inline comments
  • Total number of doc strings
  • Number of function doc strings
  • Number of functions
  • Number of keywords used
  • Number of lines
  • Number of characters
  • Number of multiple exit functions

It also prints out a list of functions with doc strings present (indicated by a '+' sign against the function name) or missing (indicated by a '-' sign against the function name).

The next metric is a list of cyclomatic complexity index for each function in the module. PyMetrics will do this rigorously for every class method and every module-level function. At the end it prints a COCOMO 2 SLOC (Source Lines of Code) metrics for the entire module.

Sample run
For demo purposes, let us see what happens if we run PyMetrics on the following simple piece of code containing two Python functions.

#! /usr/bin/env python
# Module: mymodule.py

def func1(x):
    """ A test function """
    if x<0: return x
    elif x>2 and x<10: return x*x
    elif x>10: return pow(x,0.5)

def func2(x,y):
    """ A test function """
    if x>y: return (x-y)
    elif x==y: return 0
    elif y>x: return 1
}}}

Here is the output of PyMetrics.

{{{ $ PyMetrics.py mymodule.py

File: mymodule.py

Module mymodule.py is missing a module doc string. Detected at line 4

In file mymodule.py, function func1 has 3 extra exits at lines 6, 7, 8 In file mymodule.py, function func2 has 3 extra exits at lines 12, 13, 14

Basic Metrics for module mymodule.py


         2    blockCount
         1    maxBlockDepth
       293    numCharacters
         2    numComments
         2    numDocStrings
         2    numFcnDocStrings
         2    numFunctions
        15    numKeywords
        14    numLines
         2    numMultipleExitFcns
       126    tokenCount
        14.29 %Comments
       100.00 %FunctionsHavingDocStrings

Functions DocString present(+) or missing(-)


+ func1 + func2


McCabe Complexity Metric for file mymodule.py


         4    func1
         4    func2


COCOMO 2's SLOC Metric for mymodule.py


         8    mymodule.py
      • End of Run ***

}}}

The output of PyMetrics is very customizable. Instead of printing it to the console, you can save the output in a CSV file. You can also ask PyMetrics to create SQL statements which will allow to create a database containing the metrics. PyMetrics will create appropriate SQL statements and dump the output to a .sql file.


What to expect next

In the next article of this series, I will discuss metrics for Object Oriented programming in detail. Watch this space.









Most Recent

Most Popular

Most Active Categories




Back To Top Add New Article Printable Page

MediaWiki

This page has been accessed 10,662 times.

This page was last modified 19:33, 2 February 2006.