locked
CA1502 : Microsoft.Maintainability : Function has a cyclomatic complexity of 27. Rewrite or refactor RRS feed

  • Question

  • I have this warning with fxcop... and don't really know how i can fix it.

    CA1502 : Microsoft.Maintainability : Function has a cyclomatic complexity of 27. Rewrite or refactor the method to reduce complexity to 25.  

    It's a methods that has like alot of
    if/if else


    How can i refactor something like that ....



    Tuesday, October 10, 2006 3:12 PM

Answers

  • Basically this rule is stating that you should try to break this method up into a number of smaller discrete methods as it appears that it is very complex and could be trying to do too much.

    Complex methods are not only harder to maintain but are also more likely to contain bugs.

    Tuesday, October 10, 2006 3:34 PM
    Moderator
  • Without seeing the method it is hard to offer suggestions on improving it, however, typically a large switch statement or chained if...else... statements can often be replaced with the Strategy Pattern.

    Tuesday, October 10, 2006 5:53 PM
    Moderator

All replies

  • Basically this rule is stating that you should try to break this method up into a number of smaller discrete methods as it appears that it is very complex and could be trying to do too much.

    Complex methods are not only harder to maintain but are also more likely to contain bugs.

    Tuesday, October 10, 2006 3:34 PM
    Moderator
  • the thing is that it's only doing the following

    if()
    {
    call method
    }
    else if()
    {
    call method2
    }
    etc.....
    Tuesday, October 10, 2006 5:23 PM
  • Without seeing the method it is hard to offer suggestions on improving it, however, typically a large switch statement or chained if...else... statements can often be replaced with the Strategy Pattern.

    Tuesday, October 10, 2006 5:53 PM
    Moderator
  • the code is something like the following

    void foo(string name)
    {

    if(name.Equals("1"))
    {
       toto();
    }
    else if(name.Equals("2"))
    {
        toto2();
    }

    .... 26 times else if .... calling different fonctions ...
    }

    Ill see how i can adopt the strategy pattern ....
    Friday, October 13, 2006 5:27 PM
  • I've check the strategy pattern ... and it seems that i can't avoid the if/else if ..

    Cuz i still need to check the string that is passed in order to return the right context.

    therefore .. if/else if .. 26 times ...

    Context foo(string name)
    {

    if(name.Equals("1"))
    {
    return new Context(new Strategy1());
    }
    else if(name.Equals("2"))
    {
    return new Context(new Strategy2());
    }

    .... 26 times else if ....
    }


    Monday, October 16, 2006 2:31 PM
  • Just to share with you guys so you don't think that you life is bad enought.

    This is what I got when analysing other people code and a TAO to know when to suicide:

    Warning

    CA1502 : Microsoft.Maintainability :

    MethodName has a cyclomatic complexity of 380.

    Rewrite or refactor the method to reduce complexity to 25.

     

    Friday, August 24, 2007 5:49 PM
  • I wrote a post on this to solve this problem here: http://www.simonrhart.com/2009/01/ca1502-microsoftmaintainability-net.html

    --
    Simon.
    Simon Hart
    Friday, January 9, 2009 10:00 AM