KnowDotNet Visual Organizer

NET Refactor - Complexity Analyzer

How Complex Are Your Methods?

Want to Know how complex your methods are?  Use NET Refactors's Complexity Analyzer.  NET Refactor has a new tool to automatically compute the complexity factors for your methods.  Several factors are considered, including Cyclomatic Complexity, nested If, ElseIf, Case depths, and nested loop (For, While, Do) depths, for both C# and VB.NET.

The feature is found on the main NET Refactor menu and is called Complexity Analyzer.  When invoked, the Complexity Analyzer appears as shown in Figure 2.

Figure 1 shows a block of code that is being analyzed by the Complexity Analyzer.  The code is strictly for demonstration purposes and does noting useful.

Figure 1 - Complex Code with Nested Loops and Ifs.

    private static string TestCode(int i, short j)
    {
      
int l = 0;
      
string lesTest = "Test";
      
string[] s2 ={"K","L"};

      myStruct ms =
new myStruct();
      
string[] col = new string[3]{"Q","W","S"};
      TestCodeContinuation(ms.s,i,j);

      
for(int y=0;y<10;y++){int yy=y;yy=y+1;}
      
      
for (int l =0; l<10;l++)
      {
        
int k=l; /*comment*/
        ms.ss=ms.s; /*comment2
                      * col should not see
                      * comment4 */
        ms.ss=ms.s2;
        
if (k=1)
        {
          k=2;
        }
        
else
        {
          
if (k=2)
            k=1;
        }
      }

      ms.s="B";

      
switch (k)
      {
        
case(1):
        {
          k=1;
          
if(k==2)
          {
            
for (int i=0;i<10;i++)
            {
              
if(k==6)
              {
                k=2;
              }
            }
          }
          
break;
        }
        
case (2):
          
break;
        
default:
          
break;
      }

      
foreach(string s1 in col)
      {
        ms.s2 = s1;
        ms.s="Q";
        
if(match(ms, col))
        {
          ms.s2=ms.s;
        }
      }
    }

Figure 2 - Complexity Analyzer Dialog.

Complexity Analyzer Dialog


The Complexity Analyzer displays the entire solution in a TreeView, including any number of projects, which can be a mix VB.NET and C# projects.

TreeView Popup Menu


Right-clicking on the TreeView will display a popup context menu that has options to do the following:

  • Show Selection - Shows Code for Selected CodeElement

  • Analyze Selection - Shows Complexity Statistics

  • GoTo Selection - Jumps to Selected Code Element in IDE Code Window

  • Show Object Detail - Shows Detail Data of Selected Code Element>

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing