avoid deeply nested if’s, while’s (and equivalent); 2-3 deep should be enough, otherwise use separate functions

Aug 14, 2012 at 5:59 AM

Hello All,

I want to raise a violation to avoid deeply nested loops like "If", "While" , "for", "foreach" etc.

Can anyone help me in fixing this issue..



Aug 14, 2012 at 3:50 PM

In my opinion this issue is better handed by a custom Code Analysis (FxCop) rule. It's not a matter of style, but rather of structure.

I would suggest that the nesting in and of itself is not the problem, but rather the cyclomatic complexity of a method that you want to watch out for. There are tools (such as NDepend) which easily measure this without having to write any code.

Aug 21, 2012 at 5:55 AM

Hello Jalbert,

Thanks for the reply, Will try to use FxCop..


Aug 21, 2012 at 7:01 AM

You can also use Source Monitor from http://www.campwoodsw.com/sourcemonitor.html to check for Cyclomatic Complexity. I have found it very easy to use.