Avoid unmaintainable code
Avoid unmaintainable code
Microsoft docsDescription
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.
Cause
A type, method, field, property, or event has a low maintainability index value.
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.
Example
#pragma warning disable CA1505
// The code that's violating the rule is on this line.
#pragma warning restore CA1505When to suppress
You can suppress this warning when the type or method cannot be split or is considered maintainable despite its large size. You might see false positive warnings from this rule if all of the following apply:
- You're using Visual Studio 2022 version 17.5 or later with an older version of the .NET SDK, that is, .NET 6 or earlier.
- You're using the analyzers from the .NET 6 SDK or an older version of the analyzer packages, such as Microsoft.CodeAnalysis.FxCopAnalyzers.
The false positives are due to a breaking change in the C# compiler. Consider using a newer analyzer that contains the fix for the false positive warnings. Upgrade to Microsoft.CodeAnalysis.NetAnalyzers version 7.0.0-preview1.22464.1 or newer or use the analyzers from the .NET 7 SDK.