Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Development Edition
 Avoid excessive complexity
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Studio Team System
Avoid excessive complexity

Updated: November 2007

TypeName

AvoidExcessiveComplexity

CheckId

CA1502

Category

Microsoft.Maintainability

Breaking Change

Non Breaking

A method has an excessive cyclomatic complexity.

Cyclomatic complexity measures the number of linearly independent paths through the method, which is determined by the number and complexity of conditional branches. A low cyclomatic complexity generally indicates a method that is easy to understand, test, and maintain. The cyclomatic complexity is calculated from a control flow graph of the method and is given as follows:

cyclomatic complexity = the number of edges - the number of nodes + 1

where a node represents a logic branch point and an edge represents a line between nodes.

The rule reports a violation when the cyclomatic complexity is more than 25.

To fix a violation of this rule, refactor the method to reduce its cyclomatic complexity.

It is safe to suppress a warning from this rule if the complexity cannot easily be reduced and the method is easy to understand, test, and maintain. In particular, a method that contains a large switch (Select in Visual Basic) statement is a candidate for exclusion. The risk of destabilizing the code base late in the development cycle or introducing an unexpected change in runtime behavior in previously shipped code might outweigh the maintainability benefits of refactoring the code.

Basically, the cyclomatic complexity is calculated by adding 1 to the following:

  • Number of branches (such as if, while, and do)

  • Number of case statements within a switch

The following examples show methods with varying cyclomatic complexities.

Cyclomatic Complexity of 1

Visual Basic
Public Sub Method()
    Console.WriteLine("Hello World!")
End Sub

C#
public void Method()
{
    Console.WriteLine("Hello World!");
}

Visual C++
void Method()
{
    Console::WriteLine("Hello World!");
}

Cyclomatic Complexity of 2

Visual Basic
Public Sub Method(ByVal condition As Boolean)
    If (condition) Then
        Console.WriteLine("Hello World!")
    End If
End Sub

C#

void Method(bool condition)
{
    if (condition)
    {
        Console.WriteLine("Hello World!");
    }
}

Visual C++
void Method(bool condition)
{ 
  if (condition)
    { 
        Console::WriteLine("Hello World!"); 
    } 
}

Cyclomatic Complexity of 3

Visual Basic
Public Sub Method(ByVal condition1 As Boolean, ByVal condition2 As Boolean)
    If (condition1 OrElse condition2) Then
        Console.WriteLine("Hello World!")
    End If
End Sub

C#
public void Method(bool condition1, bool condition2)
{
    if (condition1 || condition2)
    {
        Console.WriteLine("Hello World!");
    }
}

Visual C++
void Method(bool condition1, bool condition2)
{
    if (condition1 || condition2)
    {
        Console::WriteLine("Hello World!");
    }
}

Cyclomatic Complexity of 4

Visual Basic
Public Sub Method(ByVal day As DayOfWeek)
    Select Case day
        Case DayOfWeek.Monday
            Console.WriteLine("Today is Monday!")
        Case DayOfWeek.Tuesday
            Console.WriteLine("Today is Tuesday!")
        Case DayOfWeek.Wednesday
            Console.WriteLine("Today is Wednesday!")
        Case DayOfWeek.Thursday
            Console.WriteLine("Today is Thursday!")
        Case DayOfWeek.Friday
            Console.WriteLine("Today is Friday!")
        Case DayOfWeek.Saturday
            Console.WriteLine("Today is Saturday!")
        Case DayOfWeek.Sunday
            Console.WriteLine("Today is Sunday!")
    End Select
End Sub


C#
    public void Method(DayOfWeek day)
    {

        switch (day)
        {
            case DayOfWeek.Monday:
                Console.WriteLine("Today is Monday!");
                break;
            case DayOfWeek.Tuesday:
                Console.WriteLine("Today is Tuesday!");
                break;
            case DayOfWeek.Wednesday:
                Console.WriteLine("Today is Wednesday!");
                break;
            case DayOfWeek.Thursday:
                Console.WriteLine("Today is Thursday!");
                break;
            case DayOfWeek.Friday:
                Console.WriteLine("Today is Friday!");
                break;
            case DayOfWeek.Saturday:
                Console.WriteLine("Today is Saturday!");
                break;
            case DayOfWeek.Sunday:
                Console.WriteLine("Today is Sunday!");
                break;
        }
    }

}

Visual C++
void Method(DayOfWeek day)
{
    switch (day)
    {
        case DayOfWeek::Monday:
            Console::WriteLine("Today is Monday!");
            break;
        case DayOfWeek::Tuesday:
            Console::WriteLine("Today is Tuesday!");
        break;
        case DayOfWeek::Wednesday:
            Console::WriteLine("Today is Wednesday!");
        break;
        case DayOfWeek::Thursday:
            Console::WriteLine("Today is Thursday!");
        break;
        case DayOfWeek::Friday:
            Console::WriteLine("Today is Friday!");
        break;
        case DayOfWeek::Saturday:
            Console::WriteLine("Today is Saturday!");
        break;
        case DayOfWeek::Sunday:
            Console::WriteLine("Today is Sunday!");
        break;
    }
}

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker