Home > C#, Code Analysis, Visual Studio > Quickly, run!! It’s the Cyclomatic!

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!

Share

Advertisements
  1. September 29, 2011 at 5:34 am

    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

    • September 30, 2011 at 5:08 pm

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

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: