GNU Complexity Command Line Tool Measures Complexity of C Code

GNU complexity is a command line tool that computes a complexity measure of C source code, similar to pmccabe, but with a different method of calculating results with short functions scoring lower than pmccabe and highly nested functionality can score considerably higher. It can be useful to locate suspicious areas in unfamiliar code, get an idea of the efforts required to either understand the code or test it, or self-assess your own code.

Bruce Korb, the maintainer, has just released version 1.5 with some bug fixes, so I’ve given it a quick try.

We’ll need to get the code, build and install it first:

The user’s manual provides some insights and an example, which I’ve used against a directory in Linux source code:

The resulting table shows six information per line: the computed score, the number of lines between the opening and closing curly braces (ln-ct), the number of non-comment, non-blank lines found there (nc-lns), the name of the source file, the line number of the opening curly brace, and the name of the procedure. The higher the score the more complex is the procedure, and thres parameter is used to remove the lowest complexity function from the list. The ranking goes as follows:

  • ‘0-9’ – Easily maintained code.
  • ‘10-19’ – Maintained with little trouble.
  • ‘20-29’ – Maintained with some effort.
  • ‘30-39’ – Difficult to maintain code.
  • ‘40-49’ – Hard to maintain code.
  • ‘50-99’ – Unmaintainable code.
  • ‘100-199’ – Crazy making difficult code.
  • ‘200+’ – I only wish I were kidding.

Have fun.

Support CNX Software - Donate via PayPal or become a Patron on Patreon

Leave a Reply

4 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
2 Comment authors
cnxsoft Recent comment authors
newest oldest most voted
Notify of

This was news to me. Thanks so much for sharing.


@cnxsoft, which font is used in the shell (output)? Is it a monospaced font?