Archive

Posts Tagged ‘ndepend’

Quickly, run!! It’s the Cyclomatic!

September 28, 2011 2 comments

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