CA1502 : Microsoft.Maintainability : Function has a cyclomatic complexity of 27. Rewrite or refactor
-
Tuesday, October 10, 2006 3:12 PMI 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 ....
Answers
-
Tuesday, October 10, 2006 3:34 PMModerator
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 5:53 PMModerator
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.
All Replies
-
Tuesday, October 10, 2006 3:34 PMModerator
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 5:23 PMthe thing is that it's only doing the following
if()
{
call method
}
else if()
{
call method2
}
etc..... -
Tuesday, October 10, 2006 5:53 PMModerator
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.
-
Friday, October 13, 2006 5:27 PMthe 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 .... -
Monday, October 16, 2006 2:31 PMI'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 ....
} -
Friday, August 24, 2007 5:49 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, January 09, 2009 10:00 AM
I wrote a post on this to solve this problem here: http://www.simonrhart.com/2009/01/ca1502-microsoftmaintainability-net.html
--
Simon.
Simon Hart- Proposed As Answer by Simon Hart Saturday, January 10, 2009 11:30 AM

