Geeks With Blogs
Hannes Pavelka It isn't rocket science. Well, unless of course you're NASA

Managing and reducing the complexity of your software is important. Complexity makes your software more error prone and harder to maintain.
I believe in this tenet and I am a disciple of the KISS (Keep it Simple, Stupid aka Keep it super simple) principle.
However, the obvious question at hand is: How to measure complexity and thus identify where the KISS principle is violated ?

One popular method to identify complexity is to calculate the Cyclomatic Complexity (CC). 

Before talking about the tools that can help to calculate the cyclomatic complexity let me briefly explain the basics around this method.

This metric was developed by Thomas McCabe. He analyzed thousand of software projects and concluded that the reliability and stability of a program is strongly correlated to its control flow complexity.

He introduced the following formula:

M = E - N + P

M = cyclomatic complexity

E = the number of edges of the graph

N = the number of nodes of the graph

P = the number of connected components.

His paper describing this method in detail was published 1976. During that time flowcharts where pretty popular, obviously you don’t make much use of them today anymore.

A more proper way for current projects is to calculate the cyclomatic complexity using the subsequent approach:

For each occurrence of the following keywords or symbols you add one point:
 if, while, for, and (&&), or ( ||) , switch

Depending on your language syntax those keywords and symbols may vary.
More generally speaking, the following statements increase the complexity by one level:

  • conditional statement
  • looping statement
  • for each logical operator used in a condition
  • for each case in case or switch statement

If you counted all statements add one extra point and you have your cyclomatic complexity score.

Here is a simply example:

public void ShowNavbar()

{

IWorkspace workspace =Workspaces[WorkspaceNames.LeftWorkspace];

if (_Navbar != null)

{

Items.Remove(_Navbar);

}

_Navbar = Items.AddNew<NavbarView>();

workspace.Show(_Navbar);

}


This method has one if statement, so one point. Add the extra point and you get 2 as cc score.

Let’s take a look at a few tools that do analyze your code and provide the cyclomatic complexity score.

Needless to say, cyclomatic complexity is only one metric those tools offer. The other metrics (e.g Lines Of Code (LOC), perentage of comments, etc…) are equally useful and you get most out of the tools if you identify those metrics most valuable for you.

SourceMonitor from Campwood Software

One of my favorite tools to collect metrics for a software project is called SourceMonitor from Campwood Software.

As you can see from this screenshot SourceMonitor neatly display statistics about your projects including the cyclomatic complexity.

Another great feature I love is the possibility to produce kiviat charts.
Those charts do provide an easy way of viewing your projects metrics. Shown below as an example of an Kiviat chart from my current project:

With the help of the Kiviat diagram you can easily tell how well your source code meets quality guidelines for a group of metrics. You will be able to select which metrics are included in the diagram as well as the upper and lower values that should be used to define quality for each metric.

Reflector Code Metrics Addin

Another tool which is great for codemetrics is a Reflector Addin developed by Jonathan de Halleux.

You can download it via projectdistributor.net

The outstanding feature is the ability to write your own metrics without recompiling the PlugIn.

You find an easy to follow explanation how to write your own metric here.

Other tools

Personally I have only used SourceMonitor and the reflector addin for calculating metrics. However, when talking about this posting with co-workers and friends they pointed me to some other good free products.

I list them here, so you can check them out. If you know about any others drop a comment please !

VIL

vil provides metrics, visualization, querying, and analysis of .NET assemblies, classes, and methods for all .NET languages, including C# and Visual Basic.NET

NDepend

NDepend

NDepend analyses .NET assemblies of an application and generates design quality metrics. NDepend allows you to automatically measure the quality of a design in terms of its extensibility, reusability and maintainability to effectively manage and control the assemblies’ dependencies of your .NET applications. Moreover, NDepend helps you to get a thorough view of the topology of your application, at component at type and at member level.

Posted on Saturday, February 17, 2007 9:02 AM Misc | Back to top


Comments on this post: How to manage and handle complexity using code metrics

# re: How to manage and handle complexity using code metrics
Requesting Gravatar...
That's very nice man .. thanx alot
Left by AbuZooz on May 03, 2007 6:37 PM

Your comment:
 (will show your gravatar)


Copyright © Hannes Pavelka | Powered by: GeeksWithBlogs.net