Measure ‘complexity’ of C programs

hacker_compComplexity is a GNU utility to calculate the complexity of a C program. It’s similar to the McCabe scoring (ref tool: pmccabe) with extra considerations on the scoring scheme. Complexity can be used as a means to analyze a C program and detect areas to improve or simplify or the amount of effort required to understand a piece of code. Complexity is written in C.

Information from complexity analysis help to:

  • locate suspicious areas in unfamiliar code
  • get an idea of how much effort may be required to understand that code
  • get an idea of the effort required to test a code base
  • a measure of how easy it is for others to underand your own code

Complexity considers the following additional aspects compared to pmccabe:

  • code length
  • switch statement
  • logic conditions
  • author’s personal experience
  • rationale summary

Code complexity computation involves several parsing methods and a robust complexity measurement algorithm.


Download the code and install it:

$ sudo apt-get install autogen
$ tar -xvf complexity-version.tar.xz
$ cd complexity-version
$ make
$ sudo make install


A sample run of complexity on dslib:

$ complexity --histogram --score --thresh=3 *.c
Complexity Scores
Score | ln-ct | nc-lns| file-name(line): proc-name
    6      43      35   tree.c(94): insert_tree_node
    9      59      52   avl.c(136): rebalance
    9      65      55   avl.c(362): delete_avl_node
   10      75      52   avl.c(492): search_BFS_avl
   10      75      52   tree.c(268): search_BFS
   15      66      53   tree.c(144): delete_tree_node
   17      88      61   tree.c(358): search_DFS
   23      87      61   avl.c(267): insert_avl_node

Complexity Histogram
Score-Range  Lin-Ct
    0-9         142 ***************************************
   10-19        218 ************************************************************
   20-29         61 *****************

Scored procedure ct:        8
Non-comment line ct:      421
Average line score:        13
25%-ile score:              9 (75% in higher score procs)
50%-ile score:             10 (half in higher score procs)
75%-ile score:             17 (25% in higher score procs)
Highest score:             23 (insert_avl_node() in avl.c)

Score meanings

For further help, run:

$ complexity --help

Complexity scale

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


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s