Automated check for nested statements in Visual Studio

112 views Asked by At

As part of my apprenticement, I have to find a way to (automatically) search for statements nested more than 4 levels down.

I use Visual Studio (2012) and C# programming language.

An example of a (faulty) nested method.

foreach (int i in items) //1
{
    Console.WriteLine(i);
    foreach (int a in items2) //2
    {
        Console.Write(a);
        foreach (int b in items3) //3
        {
            Console.Write(b);
            foreach (int c in items4) //4
            {
                Console.Write(c);
                foreach (int d in items5) //5
                {
                    // Here an error/warning should be shown because it is nested too deep
                    Console.Write(d);
                    foreach (int e in items5)
                    {
                        Console.Write(e);
                    }

                }
            }
        }
    }
}

I have tried using ReSharper(7.1) but this does not (to my knowledge) offer this feature.

How can I achieve this?

1

There are 1 answers

0
jessehouwing On

Resharper can check this, but you will probably need to build a custom Plugin for it, as could a DxCore plugin (CodeRush) from Developer Express. StyleCop or FxCop (Code Analysis) can also check something like this in an automated way using a custom written rule.

There's also a CTP out for Roslyn, the new compiler and parser framework for Visual Studio that may ship with the next version of the product. Roslyn also allows you to inspect the code and add warnings or provide fixes.

Try any of these and if you run into issues, either update this post with more details on what you've tried and what works or doesn't or post a new question.