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!

cc_before

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:

cc_after

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.

I Am Not Now, Nor Have I Ever Been

Last weekend I was stopped behind several other cars at a traffic light, when the guy behind me in a bright-yellow low-end muscle car lays on his horn. I didn’t think anything of it, except that he ought to learn to relax. The light changes, and we all start to move. Yellow-car-guy races to pull up next to me, and flips me the bird. Huh. I wonder what has his panties in a bunch?

Then he pulls in front of me, and again flips me off, this time through his open sunroof. Still no idea what his malfunction is. But it’s a nice sunny day, and the car he’s driving suggests that he’s compensating for some shortcoming, so I shrug it off.

A mile or so later, I pull into a turning lane and pass yellow-car-guy. As I go by he sticks his shaved head out of his car window and flips me off yet again. And this time he yells "Fucking Communist!"

Aha. Now I get it. I have an Obama 2012 bumper sticker on my car. Like it or dislike it, I think you’ll agree it’s a fairly trivial expression of opinion, all things considered. But clearly, that’s what has muscle-boy so worked up.

I’m not sure what the inbred skinhead racist moron thought he was accomplishing (hey, if he can conclude that I’m a fucking communist on the basis of a bumper sticker, then surely I can reasonably conclude he’s an inbred skinhead racist moron on the basis of his reaction to it). But I do know what he actually accomplished – he put a grin on my face that lasted for the better part of an hour. If by merely having a simple bumper sticker I can make such a cretin so angry that he has to scream obscenities at a complete stranger on the road, well, that makes me very happy.

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!