Cyclomatic Complexity Measurements with pmccabe

A common question that I often receive is “What McCabe Cyclomatic Complexity tools should I use?” Now this is always a partially loaded question because while there are quite a few out there that I really like, most developers are really asking what free tools I can recommend to them. One of my favorite ones that I’ve been using that is free, open source and completely command line based is pmccabe.

pmccabe is a cyclomatic complexity measurement tool that has been used by Hewlett Packard all the way back since the early 1990’s for C and C++ code. The tool is ran from a command line in Linux or MacOS (or could possibly be ran on Windows from Cygwin but I have not attempted this).  First though it needs to be installed using either of the following command lines based on the operating system used:

For Mac:

brew install pmccabe

For Linux:

sudo apt-get update -y

sudo apt-get install -y pmccabe

Once the cyclomatic complexity tool is installed, running a file through the tool is as simple as navigating to the directory with your source and typing:

pmccabe *

Now for a quick example in today’s post, let’s look at a communication decoding state machine file that I often use. The file is named packet.c so we can use the following command to measurement the cyclomatic complexity:

pmccabe -v packet.c

Now I’m using the -v switch so that the tool will print the column headers so that I know what each column means. The result would look something like the following:

As we can see, I followed good programming practices and kept all my functions below a cyclomatic complexity of 10.

One thing that I really like about this tool is that it can be easily integrated into a Linux based build server and doesn’t require much from a scripting standpoint to run the tests, and then parse them for results. In most instances I only want to see which functions in a code base have complexities greater than 10 and I often want to display them in groups from 11 – 20, 21 – 30, 31 – 40 and 40+.

With how easy this tool is to use and the fact that it is open source, developers really don’t have an excuse to not monitor how complex their software is becoming. (Even on Windows, you can install Ubuntu on a Virtual Box VM to run it alongside a test harness.) So if you are looking for a great tool for cyclomatic complexity, this one might just do the trick for you.

Share >

4 thoughts on “Cyclomatic Complexity Measurements with pmccabe

  1. I’m a bit skeptical concerning cyclomatic complexity metrics. First of all, the hight complexity rating a ‘switch/case’ structure gets. This may be formally justified, but they can be easily read, understood and maintained by humans.
    When designing with non-blocking finite state machines, I don’t even know how not to use them without messing up the code.
    For larger projects, an RTOS is fine but with smaller projects (e.g. something that fits into an ATtiny) it would be overengineered.

    • That is one thing I like about pmccabe, it displays the traditional complexity measurement along with the modified measurement which treats a switch case as 1. Personally I use it to help guide test case development for independent linear paths through the function.

    • I’ve not used that one. I will have to try it out. (I plan to do a 5 Complexity Measurement tools post at some point in the future).

Leave a Reply to H.-P. Völpel Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.