Sunday, November 9, 2014

Cyclomatic Complexity ( CC )

One day  I was required to put application inside sonar for measuring code management percentage,and I found the term "Cyclocmatic complexity".

If you ask "why I should manage my code, the code is working fine".
I will tell you yes this is the good news but the bad news is your code now is unmanageable.
  • Benefits of Code Management:
Code management make easier to deal with code at the time of maintenance,
Code management may have improve the performance of your application.

Now lets's see:
  • What is the meaning of this term "Cyclomatic Complexity" ?
The Cyclomatic Complexity is a  measured by the number of checking for a decision to be made + 1.
Cyclocmatic complexity = Number of decision points + 1.

(&&, ||) operators and (if, while, do, for, ?:, catch, switch, case, return, throw) statements in the body of a class plus one for each constructor, method (but not getter/setter), static initializer, or instance initializer in the class and the last return statement in method, if exists, is not taken into account.

Example 1:
 

String str = "someString";

if ( str.equals( case1 ) )

do something;

if( str.equals( case2 ) )

do something;

else

do default thing;




Cyclomatic Complexity for previous code is = ( if for case1 ) + ( if for case2 ) + ( else ) +  1  =  4

Example 2:
 

if( name.equals(name1) || name.equals( name2 ) || name.equals( name3) && age != 23 )

{ do something }




Cyclocmatic complexity = if  +  ||  +  ||  +  &&  + 1 = 5
  • Avoiding Cyclomatic Complexity:
In that factory design there may be either switch case or several if else conditions.

Example:
at package model we write an interface , two classes implements it and a factory class.
 

public interface Handler

{

 public void handle();

}



public class AHandler implements Handler

{

 public void handle()

 {

  System.out.println("A handler");

 }

}



 

public class BHandler implements Handler

{

 public void handle()

 {

  System.out.println("B handler");

 } 

}



 

public class AbstractHandler

{

public static Handler getHandler( String handlerName )

 {

  Handler handler = null;

  try

  {

   if( handlerName.equals("A"))

    handler = new AHandler();

   if( handlerName.equals("B") )

    handler = new BHandler();

  }

  catch( Exception e )

  {

   System.out.println("There is no specific handler");

  }

  return handler;

 }



}



 

public class TestDynamicHandler

{

 public static void main(String[] args)

 {

  Handler handler = AbstractHandler.getHandler("B");

  handler.handle();

 }



}


In the above examples, there is nothing wrong in code,
but for every new case, you have to write a new class and you have to add one or more if clause in the class “AbstractHandler”.

Improvement is to modify class “AbstractHandler” in the following manner:

 

public class AbstractHandler

{

public static Handler getHandler( String handlerName )

 {

  Handler handler = null;

  try

  {

   handler = (Handler) Class.forName(

     "model." + handlerName + "Handler")

     .newInstance();

  }

  catch( Exception e )

  {

   System.out.println("There is no specific handler");

  }

  return handler;

 }

}


Now no need to update the class "AbstractHandler" if you make a new class and
Cyclomatic Complexity=0.

No comments:

Post a Comment

java - fill distinct objects in ArrayList

If you have a list that contains some objects that it is considered as duplication when two or three fields of these objects are equal. How ...