C#idiomatic C#
ca1505fundamentals/code-analysis/quality-rules

Code analysis

CA1505: Avoid unmaintainable code

Learn about code analysis rule CA1505: Avoid unmaintainable code

CA1505 Maintainability enabled by default in .NET 10: No
Fix is breaking or non-breaking Non-breakingDefault threshold 10Applicable languages C# and Visual Basic

Cause

A type, method, field, property, or event has a low maintainability index value.

Rule description

The rule reports a violation when the maintainability index of a type, method, field, property, or event is less than 10. However, you can configure the threshold.

The maintainability index is calculated by using the following metrics: lines of code, program volume, and cyclomatic complexity. (Program volume is a measure of the difficulty of understanding of a type or method that's based on the number of operators and operands in the code. Cyclomatic complexity is a measure of the structural complexity of the type or method. You can learn more about code metrics at Measure complexity and maintainability of managed code.

A low maintainability index indicates that a type or method is probably difficult to maintain and would be a good candidate to redesign.

How to fix violations

To fix this violation, redesign the type or method and try to split it into smaller and more focused types or methods.

When to suppress warnings

You can suppress this warning when the type or method cannot be split or is considered maintainable despite its large size.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

csharp
#pragma warning disable CA1505
// The code that's violating the rule is on this line.
#pragma warning restore CA1505

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

ini
[*.{cs,vb}]
dotnet_diagnostic.CA1505.severity = none

For more information, see How to suppress code analysis warnings.

Configure threshold

You can configure the threshold at which this rule fires and the kinds of symbols to analyze. The allowable symbol kinds are:

  • Assembly
  • Namespace
  • Type
  • Method
  • Field
  • Event
  • Property
  1. Create a text file named CodeMetricsConfig.txt.

  2. Add the desired threshold to the text file in the following format:

    txt
    CA1505: 20

    In this example, the rule is configured to fire when the maintainability index of a type, method, field, property, or event is less than 20.

    txt
    CA1505(Method): 5
    CA1505(Type): 15

    In this example, the rule is configured to fire when the maintainability index of a method is less than 5 or the maintainability index of a type is less than 15. With this configuration file, the rule will continue to flag fields, properties, and events whose maintainability index is less than the default threshold (10).

  3. In the project file, mark the build action of the configuration file as AdditionalFiles. For example:

    xml
    <ItemGroup>
      <AdditionalFiles Include="CodeMetricsConfig.txt" />
    </ItemGroup>

See also