Skip to main content

On cyclomatic complexity

Not all of us are lucky to work with greenfield projects; the majority end up working with codebases that we inherit. These codebases have had their share of modifications that moved them away from the original elegant design: bug fixes, feature additions, and enhancements that were done in a hurry because of delivery pressure, without regard to code hygiene and future maintainability. The code then feels heavy, complex, hard to understand and more painful to maintain.

There are a lot of ways to characterize code complexity, but the one that jumps to mind is "cyclomatic complexity" described by the McCabe paper from 1976 http://www.literateprogramming.com/mccabe.pdf.  The paper describes how to measure the cyclomatic complexity of different program control paths, and recommends a bound of 10 for manageable code complexity. NIST also selects the same number http://hissa.nist.gov/HHRFdata/Artifacts/ITLdoc/235/chapter2.htm. The numbers, despite being arbitrary, provide a guideline to when to get out the scalpel, and start cleaning up the code.

I was curious about how our code fares, and luckily I found a lot of open source tools that help with that. For C/C++ programs "cccc" is good. If you're using the ports systems,

sudo port install cccc

does the trick. For Java code there are a lot of plugins for various IDEs, and for NetBeans, Simple Code Metrics is good. The plugin page shows that it is old and unmaintained, however it works perfectly in NetBeans 7.1. You choose a file or a package, and click the SCM icon, and it spits out metrics about the code including the cyclomatic complexity.

When I ran the tests, the results were an eye-opener. The good news is that most of the code has a complexity below 15, with the occasional function that is high up in the 30s. Not alarming, but definitely would need attention to keep the code hygiene high. The surprise came when I included some of the open source libraries that we use, and the numbers shot up as high as 206. When I talked to colleagues from other companies, the experiences were similar--most of the normal code hovers around 20, with the occasional function around 50, but no high alarming numbers like the 206 one.




Comments

Popular posts from this blog

Virtual machine could not be started because the hypervisor is not running

I wanted to experiment with TensorFlow, and decided to do that in a Linux VM, despite the fact that Windows Subsystem for Linux exists. In the past I used Sun’s, and then Oracle’s VirtualBox to manage virtual machines, but since my Windows install had Hyper-V, I decided to use that instead. The virtual machine configuration was easy, with disk, networking, and memory configurations non-eventful. However when I tried to start the virtual machine to setup Ubuntu from an ISO, I was greeted with the following error: “Virtual machine could not be started because the hypervisor is not running” A quick Internet search revealed that a lot of people have faced that problem, and most of the community board solutions did not make any sense. The hidden gem is this technet article , which included detailed steps to find if the Windows Hypervisor was running or not, and the error message if it failed to launch. In my case, the error was: “Hyper-V launch failed; Either VMX not present or

Why good customer service matters?

I am not an Apple fan, but I do like their computers, and recommend them to colleagues and friends for a variety of reasons. They are well designed, and in addition to an excellent user interface, they run a flavor of Unix--which makes the life of computer programmers a lot easier. But most importantly, Apple's customer support is impeccable, that despite all the hardware issues I experienced in the past, I still recommend Apple computers. Let me explain why. A year and a half ago, I bought a Mac Book Pro for work. At the time it was the first generation unibody laptop, that had an i7 processor, lots of memory, and lots of disk space. Alas, like first generation models everywhere, it also had a lot of hardware problems. The most annoying of which was the screen randomly turning dark, with the hard drive spinning out of control. The only way to get out of this state was by forcing a reboot by holding down the power button, and losing everything I have been working on. At first

MacOS Catalina, OneDrive, and case sensitive file systems

Over the weekend, I dusted off my old Macbook Air to search for some old family photos. I have not used the laptop for a long time, and it was completely out of charge. I plugged it in, and it quickly booted. Shortly after, I got bombarded with notifications that many of the applications needed updating, and that a new version of the OS was available.   I waited till I found the photos I was looking for, before attempting to upgrade anything. I also wanted to install OneDrive to get my old files to the cloud, so that I can access them from any of my devices, instead of dusting off old computers to get to them. The MacOS upgrade experience has always been fantastic, and this one was no different. The OS upgrade files downloaded quickly and after a restart and a quick install, the Macbook Air was ready to go.   Upgrading the installed applications was also a breeze, however in the process I discovered that a large majority of the applications installed were not compatible with Cata