Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Cyclomatic Complexity Measurements: Simple code == Better application

We all know that simpler is better when it comes to software development.  Any time there is code that is overly complex it makes it hard to test/debug and it is much harder to interpret in the future when changes/enhancements need to be made.  There are a few different ways that developers can attempt to keep their code simple.  We can use techniques such as Functional Decomposition, we can conduct code reviews, TDD, adopt pair programming, etc.

There is another way we can attempt to keep our code simple.  We can measure our code based on the Cyclomatic Complexity measurement.  This is a software metric that attempts to measure the ‘pathways’ through a method/module (read the wiki for a better definition).  [Google Search on Cyclomatic Complexity]

One way to measure the Cyclomatic Complexity of your application is to use a existing tool.  I have chosen to use a plugin for Reflector by Lutz Roeder.

After you have downloaded/installed the plug-in, fire up Relector (this assumes prior knowledge of how to use Reflector) and load the assembly you wish to view.

In order to run the code metrics you need to select the assembly from the tree view in Reflector then go to Tools -> Code Metrics.  This will open up a new panel with the different possible assemblies to run the metrics against.  Check the one you want and click the refresh icon in the tool bar.  This will run analysis for your assembly.  When this is done, it will give a listing of the results for each class.  If you wish to view the results on a per method level, simply change drop down to ‘Method Metrics’ to refresh the list for methods.  You will now be able to sort the results any way you want.  I like to sort by the CyclomaticComplexity rating.

If you see methods with a rating of 10 or higher you may want to spend a few minutes to see if there is any way to improve this logic.  The goal would be to have no methods with a complexity of 50 or higher, while having most of your methods have a complexity of 20 or less.

Complexity Threshold Values
1 - 10: Simple Program, without much risk

11-20: More complex program, moderate risk
21-50: Complex program, high risk
> 50: Untestable program, very high risk


I like to spend a few minutes here and there to run this on my applications in order to get a view of what is going on.  Learning to use this tool effectively will make your code base simpler, and your application better.  Both of these are keys to success in the software world.

Enjoy…


Posted 05-08-2007 7:19 AM by Derik Whittaker

[Advertisement]

Comments

nielsvk wrote re: Cyclomatic Complexity Measurements: Simple code == Better application
on 05-09-2007 4:27 AM

I extracted the contents of both zip files to one directory, starting with Reflector. When selecting an assembly inside Reflector, the Tools menu doesn't show the CodeMetrics plug-in. What could be wrong?

Derik Whittaker wrote re: Cyclomatic Complexity Measurements: Simple code == Better application
on 05-09-2007 7:17 AM

Nielsvk,

You need to 'setup' the plug-in in Reflector.   Here is how u do this.

1) Open Reflector

2) Go to View - Add-ins (this opens the Add-Ins dialog)

3) Click add, find the Reflector.CodeMetrics.dll assembly on your drive.

4) Click Close on the Add-Ins dialog

5) Go to the Tools menu and you should see the menu option for Code Metrics.

Hope this helps.

Derik

frogsbrain wrote re: Cyclomatic Complexity Measurements: Simple code == Better application
on 05-09-2007 8:01 PM

Hmm.  I remember running this same analysis against a certain point-of-sale system at a prior employer, and the results were... startling.  Let's say if > 50 is un-testable then > 100 has reached ludicrous and > 150 has gone to plaid.  I heard Lovecraft wrote of a system much like that one once. ;-)

nielsvk wrote re: Cyclomatic Complexity Measurements: Simple code == Better application
on 05-10-2007 4:14 AM

Thanks Derik! It works fine now thanks to you.

Derik Whittaker wrote re: Cyclomatic Complexity Measurements: Simple code == Better application
on 05-10-2007 8:24 AM

Frogsbrian,

Do we know each other?

DamonCarr wrote re: Cyclomatic Complexity Measurements: Simple code == Better application
on 05-20-2007 7:50 AM

I have been using this since I first found it... Nice to see another person recognizes the value here!

I am the guy they quote on the product home page...

Damon

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)