Cyclomatic Complexity

Cyclomatic Complexity is one of those programmer geek things that I concern myself with that I’ve found many other developers don’t care about, or perhaps aren’t even aware of.

For years I’ve been using SourceMonitor from Campwood Software to keep an eye on the various projects I work on, and gauge where the code could use some TLC.

The formal definition of cyclomatic complexity is rather esoteric, but simply put it can be looked at as a way of measuring how ‘messy’ your source code is. The messiness of the code is a decent indicator of how difficult the code is to maintain, how fragile it is, and how likely you are to break it when you make a change.

A commonly accepted ideal limit to complexity in a given chunk of code is 10. That is, code that has a measured complexity of 10 or less is easy to understand, maintain, debug and modify. In my own experience, a complexity value of up to 20 is pretty workable (and sometimes unavoidable). As long as the code is consistently formatted, has good naming conventions, and is clearly documented with comments, you can live with it.

When I started my current job almost three years ago, we inherited a large, organic code base. To my surprise, the average cyclomatic complexity was quite low, just over 3. To my much greater surprise, the most complex method was 448!


Three years ago I was new to this code base, and had larger concerns. I looked at the routine in question, but since I didn’t understand what it was doing, or its importance in the system, I left it alone, but kept it in the back of my mind.

A couple of months ago I had a task that involved modifying this highly complex routine, so I took the time to really go through it and understand what it was doing. I found that it was actually something fairly simple – looking through text for variable tokens and replacing them with real values – but it was doing a whole heck of a lot of it. Hence the complexity number.

I also found that it was doing some text formatting, which is a different sort of operation. Aha! Here was an opportunity to perform an Extract Method refactoring. The result was a significant reduction in the complexity value, from 448 down to 357:


The newly extracted method had a complexity of 90, which is about what you’d expect – the complexity isn’t removed from the the code, after all. Instead it’s broken up into smaller, less complex pieces.

Now, 357 (and 90) are still very high cyclomatic complexity values, given that anything over 20 or so is considered dangerously complex and fragile. But in the case of these routines, the complexity value is misleading. The routines are just long series of “if the text says this, change it to that” operations. They’ve got a huge number of such choices, but in actual practice the code is easy to read, understand and modify.

Lesson: cyclomatic complexity is a metric worth keeping tabs on, but a bad CC value doesn’t always mean bad code.

Happy with SmartBear Software

Yesterday I wrote a blog entry about my irritation with SmartBear Software over the difficulties and delays I encountered in trying to get update pricing on their AQtime product. The whole situation wouldn’t have irritated me so much if it weren’t for the fact that I really do like the program quite a lot.

The situation has improved dramatically. This morning, first of all, I received several emails from the salesman I’d spoken to a week ago; he was very apologetic and got me the pricing information I’d been looking for. Considering that I was looking to renew a license that was six years out of date, the price was actually quite reasonable.

Even more encouraging than that, though, was the reaction to my disgruntled blog posting. Apparently the folks at SmartBear monitor for any mentions of their company and products, because a short time afterwards, I was personally contacted by Eric Sargent, the Sales Director at SmartBear. He too was very apologetic over the situation, and he went well above and beyond what was necessary to ensure that it was resolved and I was happy.

And I am happy. That they noticed my blog post so quickly tells me they’re actively watching for mentions of their company, and taking immediate corrective action when a customer isn’t satisfied. They took the time to email and call me, where many businesses might have just written me off as a garden-variety internet crackpot. And they more than made up for the irritation I felt. Clearly, SmartBear does care a great deal about their reputation, and they know that it depends on regular rank-and-file coders like me. They treated me very well and made me feel important, and you know what? That’s a nice feeling!

So now I’m looking forward to having AQtime back in my bag of programming tricks, and seeing what cool new features they’ve added in the four versions I’ve skipped. Seriously, the software was pretty amazing in v3, so I anticipate great things in v7. And I’m thinking that a bunch of my projects could benefit from a good bit of profiling. I may not have any glaring issues in my code, but no doubt there are plenty of areas that could be improved upon, and AQtime is the ideal tool for finding them.

So, thank you SmartBear Software, for restoring my high opinion of you!

(No Longer) Irritated with SmartBear Software

(For the happy ending to this rant, see the next day’s post…)

SmartBear Software has a product called AQtime Pro, which is a profiler capable of profiling software at the source code line level. If you’re not a programmer, that won’t mean anything, but if you write code, you know that from time to time you’ll have a bottleneck somewhere in a complex program which you cannot track down. This is where AQtime comes in. It lets you see exactly how many times each line of code is executing, and how much time is spend on each line and routine. It is hands-down the best tool for doing this sort of detective work.

I’ve used it a couple of dozen times in the years I’ve had it, and on each of those times it has saved my bacon, quickly identifying exactly what code is causing my program to slow to a crawl.

About two weeks ago I had just that kind of a problem that needed sorting out, so I fired up my old copy of AQtime 3.16. After some struggling, I discovered that it won’t profile software written in Delphi XE. This is not at all unreasonable – that version came out in 2005 or so, and there’s no reason to expect that it could cope with the output of the very latest Delphi compiler. (Delphi XE comes with a limited version of AQtime that doesn’t profile to the line level, so it wouldn’t do what I needed). No problem, I figured, I’ll just upgrade. It is, after all, a good tool to have available.

So on 7/28/2011 I went to the SmartBear website, and logged in. Their system still had me in their records, showing that the last version I had a license to is 3.16. All well and good. I clicked the link for upgrade pricing…

… and instead of an upgrade price list it takes me to a web form that I have to fill out. Really SmartBear? You can’t just tell me on the website what my cost will be to upgrade? I buy a lot of programming tool updates online and none of those companies make me fill out a web form to get a price list!

Well, I needed the software, so I filled out the form. That was a Thursday night. The following Monday afternoon, 8/01/2011, I got a call from a salesman. I explained that my version was long out of date, and I had an immediate need for the software, and I wanted to know what an upgrade would cost me. He understood, and said he’d email me the price. What? Huh? You can’t just tell me the price over the phone?

Fine. Whatever. I put SmartBear in my spam blocker white-list so the email wouldn’t get lost, and waited. And waited. And waited.

Here we are almost a week later, and that promised email never showed up. I’ve filled out the web form again today, but only out of idle curiosity. You see, I don’t need AQtime any more for this problem. The bottleneck had to be fixed, so I muddled through the hard way and figured out the problem on my own. I’d have preferred to have a shiny new copy of AQtime to help me narrow it down in minutes instead of hours, of course. But I didn’t have the luxury of waiting around for a whole work week for SmartBear to get back to me. Oh well.

I’ve got some questions for them:

  1. Why wouldn’t your website simply show me my upgrade price?
  2. Why couldn’t the salesman simply tell me the price over the phone?
  3. Why have I not received an email with the price almost a week later?
  4. Do you no longer want my business?

SmartBear Software, you had a motivated customer, representing repeat business, who had an immediate need for your product and who was ready to buy. Too bad you dropped the ball.