Yajuvendra Vant
Yajuvendra Vant

Reputation: 1127

How to resolve CA1502: Avoid excessive complexity?

As per code analysis result, the following is the warning message,

CA1502 Avoid excessive complexity 'METHOD()' has a cyclomatic complexity of 27. Rewrite or refactor the method to reduce complexity to 25.
BusinessServices ReportService.cs 310

What is cyclomatic complexity and how to resolve this?

Upvotes: 4

Views: 6524

Answers (2)

Ronnie Overby
Ronnie Overby

Reputation: 46490

CA1502 is a binary analyzer. In some cases your code isn't all that complex or can't be decomposed. Here is an example that resulted in a CA1502 warning:

public class Mapping : Profile
{
    [SuppressMessage("Microsoft.Maintainability", "CA1502"]
    public Mapping()
    {
        CreateMap<ItemInstance, ManifestRecords>()
            .ForMember(dest => dest.SomeProperty1, opt => opt.MapFrom(src => src.SomeProperty1))
            .ForMember(dest => dest.SomeProperty2, opt => opt.MapFrom(src => src.SomeProperty2))
            .ForMember(dest => dest.SomeProperty3, opt => opt.MapFrom(src => src.SomeProperty3))
            .ForMember(dest => dest.SomeProperty4, opt => opt.MapFrom(src => src.SomeProperty4))
            .ForMember(dest => dest.SomeProperty5, opt => opt.MapFrom(src => src.SomeProperty5))
            .ForMember(dest => dest.SomeProperty6, opt => opt.MapFrom(src => src.SomeProperty6))
            .ForMember(dest => dest.SomeProperty7, opt => opt.MapFrom(src => src.SomeProperty7))
            .ForMember(dest => dest.SomeProperty8, opt => opt.MapFrom(src => src.SomeProperty8))
            .ForMember(dest => dest.SomeProperty9, opt => opt.MapFrom(src => src.SomeProperty9))
            .ForMember(dest => dest.SomeProperty10, opt => opt.MapFrom(src => src.SomeProperty10))
            .ForMember(dest => dest.SomeProperty11, opt => opt.MapFrom(src => src.SomeProperty11))
            .ForMember(dest => dest.SomeProperty12, opt => opt.MapFrom(src => src.SomeProperty12))
            .ForMember(dest => dest.SomeProperty13, opt => opt.MapFrom(src => src.SomeProperty13))
            .ForMember(dest => dest.SomeProperty14, opt => opt.MapFrom(src => src.SomeProperty14))
            .ForMember(dest => dest.SomeProperty15, opt => opt.MapFrom(src => src.SomeProperty15))
            .ForMember(dest => dest.SomeProperty16, opt => opt.MapFrom(src => src.SomeProperty16));
    }
}

You can see I'm using AutoMapper and there's not really any cyclomatic complexity in the source code, though there may be in the compiled binary representation.

You can suppress the warning with the attribute:

[SuppressMessage("Microsoft.Maintainability", "CA1502"]

Upvotes: 2

BJ Myers
BJ Myers

Reputation: 6823

From Wikipedia:

The cyclomatic complexity of a section of source code is the count of the number of linearly independent paths through the source code. For instance, if the source code contained no decision points such as IF statements or FOR loops, the complexity would be 1, since there is only a single path through the code. If the code had a single IF statement containing a single condition, there would be two paths through the code: one path where the IF statement is evaluated as TRUE and one path where the IF statement is evaluated as FALSE.

The easiest way to resolve this is to break the method into two or more smaller methods. Visual Studio's built-in refactoring tools (such as Refactor -> Extract Method) can be used to extract a selected portion of code into another method.

Upvotes: 4

Related Questions