Wednesday, September 28, 2011

Quickly, run!! It's the Cyclomatic!

Cyclomatic....the first time I saw that word pop up in a Visual Studio Code Analysis warning, I thought I was seeing things. It sounded like some cheesy killer robot from a bad 1950's sci-fi movie. But no, it's a real thing. And it's not nearly as complicated as it sounds (no pun intended).

Cyclomatic Complexity is, simply put, a single number that represents the number of independent paths through your code. The idea is that the higher the cyclomatic complexity, the less maintainable and more error-prone your code will be. Generally, cyclomatic complexity is classified as follows:
Complexity Description
1 - 10 simple code, low risk
11 - 20 moderately complex code, moderate risk
21 - 50 complex code, high risk
51+ overly complex code, virtually untestable and unmaintainable, unacceptable risk

So what should you do with this? Try to keep you cyclomatic complexity as low as possible, that's what. A good rule of thumb is to keep it at 10 or lower, although code analysis tools like Visual Studio and FxCop won't usually complain until you hit 25 or so. Visual Studio will calculate this for you (right-click on a project, choose "Code Metrics"). However, it doesn't work for websites. There is a third-party alternative that will called NDepend, but it isn't free for commercial use. And even for academic or non-commercial use, you have to renew the free license about every month. If anyone knows of any good free alternatives, please share!


  1. In practice for academic and OSS projects, we are happy to emit free pro licenses and in returns, academic and OSS project guys are happy to help us spread the word about the product capabilities. This is a win-win collaboration :)
    Roxanne from NDepend Team

  2. That's good to know! I will likely get in touch with you soon.

  3. Good post. I learn one thing tougher on completely different blogs everyday. It can all the time be stimulating to learn content from other writers and apply slightly one thing from their store. I?d want to use some with the content material on my weblog whether or not you don?t mind. Natually I?ll provide you with a link on your net blog. Thanks for sharing.