Movatterモバイル変換


[0]ホーム

URL:


CodeNarcFork me on GitHub

General

Running

Using

Report Types

Rules

Developing

Size and Complexity Rules (“rulesets/size.xml”)

AbcMetric Rule

Calculates theABC size metric for methods/classes and checks against configured threshold values.

ThemaxMethodAbcScore property holds the threshold value for the ABC score for each method.If this value is non-zero, a method with an ABC score greater than this value is considered a violation.The value does not have to be an integer (e.g., 1.7 is allowed).

ThemaxClassAverageMethodAbcScore property holds the threshold value for the average ABCscore for each class. If this value is non-zero, a class with an average ABC score value greaterthan this value is considered a violation. The value does not have to be an integer.

ThemaxClassAbcScore property holds the threshold value for the total ABCscore value for each class. If this value is non-zero, a class with a total ABC scoregreater than this value is considered a violation. The value does not have to be an integer.

This rule treats “closure fields” as methods. If a class field is initialized to a Closure (ClosureExpression),then that Closure is analyzed and checked just like a method.

PropertyDescriptionDefault Value
maxMethodAbcScoreThe maximumABC score allowed for a single method (or “closure field”). If zero ornull, then do not check method-level scores.60
maxClassAverageMethodAbcScoreThe maximum averageABC score allowed for a class, calculated as the average score of its methods or “closure fields”. If zero ornull, then do not check class-level average scores.60
maxClassAbcScoreThe maximumABC score allowed for a class, calculated as the total ABC score of its methods or “closure fields”. If zero ornull, then do not check class-level scores.0
ignoreMethodNamesSpecifies one or more (comma-separated) method names that that should not cause a rule violation. The names may optionally contain wildcards (*,?). Note that the ignored methods still contribute to the class complexity value.null

ABC Size Metric Calculation Rules

TheABC score is calculated as follows:TheABC metric measures size by counting the number of Assignments (A), Branches (B) andConditions (C) and assigns a single numerical score calculated as:

`ABC= sqrt((AA)+(BB)+(C*C)) `

TheABC Metric calculation rules for Groovy:

Notes

ClassSize Rule

Checks if the size of a class exceeds the number of lines specified by themaxLines property.

PropertyDescriptionDefault Value
maxLinesThe maximum number of lines allowed in a class definition.1000

CrapMetric Rule

Calculates theC.R.A.P. (Change Risk Anti-Patterns)metric score for methods/classes and checks against configured threshold values.

TheCRAP metric score is based on thecyclomatic complexity and test coverage for individual methods.A method with aCRAP value greater than themaxMethodCrapScore property causes a violation. Likewise,a class that has an (average method)CRAP value greater than themaxClassAverageMethodCrapScoreproperty causes a violation.

NOTE: This rule requires theGMetrics[3] jar, version 0.5 (or later), on the classpath, as well asaCobertura[4]-[6] XML coverage file. If either of these prerequisites is not available, this rulelogs a warning messages and exits (i.e., does nothing).

ThemaxMethodCrapScore property holds the threshold value for the CRAP value for each method. If thisvalue is non-zero, a method with a cyclomatic complexity value greater than this value is considered a violation.

ThemaxClassAverageMethodCrapScore property holds the threshold value for the average CRAP valuefor each class. If this value is non-zero, a class with an average cyclomatic complexityvalue greater than this value is considered a violation.

NOTE: This rule does NOT treatclosure fields as methods (unlike some of the other size/complexity rules).

PropertyDescriptionDefault Value
coberturaXmlFileThe path to the Cobertura XML coverage file for the Groovy code By default, the path is relative to the classpath. But the path may be optionally prefixed by any of the valid java.net.URL prefixes, such as “file:” (to load from a relative or absolute path on the filesystem), or “http:”. This property is REQUIRED.null
maxMethodCrapScoreThe maximumCRAP metric value allowed for a single method. If zero ornull, then do not check method-level complexity.30
maxClassAverageMethodCrapScoreThe maximumCRAP average metric value allowed for a class, calculated as the average CRAP value of its methods. If zero ornull, then do not check the average class-level CRAP value.30
maxClassCrapScoreThe maximum totalCRAP metric value allowed for a class, calculated as the total CRAP value of its methods. If zero ornull, then do not check class-level CRAP value.0
ignoreMethodNamesSpecifies one or more (comma-separated) method names that that should not cause a rule violation. The names may optionally contain wildcards (*,?). Note that the ignored methods still contribute to the class complexity value.null

CRAP Formula

Given a Groovy method m, C.R.A.P. for m is calculated as follows:

  C.R.A.P.(m) = comp(m)^2 * (1 - cov(m)/100)^3 + comp(m)

Wherecomp(m) is thecyclomatic complexity of method m, andcov(m) is the test code coverage providedby automated tests.

References

CyclomaticComplexity Rule

Calculates theCyclomatic Complexity for methods/classes and checks against configured threshold values.

ThemaxMethodComplexity property holds the threshold value for the cyclomatic complexityvalue for each method. If this value is non-zero, a method with a cyclomatic complexity value greater thanthis value is considered a violation.

ThemaxClassAverageMethodComplexity property holds the threshold value for the average cyclomaticcomplexity value for each class. If this value is non-zero, a class with an average cyclomatic complexityvalue greater than this value is considered a violation.

This rule treats “closure fields” as methods. If a class field is initialized to a Closure (ClosureExpression),then that Closure is analyzed and checked just like a method.

PropertyDescriptionDefault Value
maxMethodComplexityThe maximumcyclomatic complexity value allowed for a single method (or “closure field”). If zero ornull, then do not check method-level complexity.20
maxClassAverageMethodComplexityThe maximum averagecyclomatic complexity value allowed for a class, calculated as the average complexity of its methods or “closure fields”. If zero ornull, then do not check average class-level complexity.20
maxClassComplexityThe maximum totalcyclomatic complexity value allowed for a class, calculated as the total complexity of its methods or “closure fields”. If zero ornull, then do not check total class-level complexity.0
ignoreMethodNamesSpecifies one or more (comma-separated) method names that that should not cause a rule violation. The names may optionally contain wildcards (*,?). Note that the ignored methods still contribute to the class complexity value.null

Cyclomatic Complexity Metric Calculation Rules

Thecyclomatic complexity value is calculated as follows:

Start with a initial (default) value of one (1).Add one (1) for each occurrence of each of the following:

 * `if` statement * `while` statement * `for` statement * `case` statement * `catch` statement * `&&` and `||` boolean operations * `?:` ternary operator and `?:` *Elvis* operator. * `?.` null-check operator

Notes

MethodCount Rule

Since CodeNarc 0.11

Checks if the number of methods within a class exceeds the number of lines specified by themaxMethod property.

A class with too many methods is probably a good suspect for refactoring, in order to reduce itscomplexity and find a way to have more fine grained objects.

PropertyDescriptionDefault Value
maxMethodsThe maximum number of methods allowed in a class definition.30

MethodSize Rule

Checks if the size of a method exceeds the number of lines specified by themaxLines property.

PropertyDescriptionDefault Value
maxLinesThe maximum number of lines allowed in a method definition.100
ignoreMethodNamesSpecifies one or more (comma-separated) method names that should be ignored (i.e., that should not cause a rule violation). The names may optionally contain wildcards (*,?).null

Known Limitations:

NestedBlockDepth Rule

Checks for blocks or closures nested more deeply than a configured maximum number.Blocks includeif,for,while,switch,try,catch,finally andsynchronized blocks/statements, as well as closures.

Methods calls, constructor calls, and property access through Builder objects are ignore. For instance, this codedoes not cause a violation:

    myBuilder.root {        foo {            bar {                baz {                    quix {                        qux {                            quaxz {                            }                        }                    }                }            }        }    }
PropertyDescriptionDefault Value
maxNestedBlockDepthThe maximum number of nesting levels. A block or closure nested deeper than that number of levels is considered a violation.5
ignoreRegexDetermines what is a builder call. For instance, closures nested on a method named createBuilder, a property named myBuilder, or a constructor call to object MyBuilder() do not produce violations..*(b|B)uilder

ParameterCount Rule

Since CodeNarc 0.23

Checks if the number of parameters in method/constructor exceeds the number of parameters specified by the maxParameters property.

Example of violations:

    void someMethod(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6) { // violation    }    @Override    void someMethod(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7) { // no violation if ignoreOverriddenMethods == true    }    class SampleClass {        SampleClass(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7) { // violation        }    }
PropertyDescriptionDefault Value
maxParametersThe maximum number of parameters in method/constructor5
ignoreOverriddenMethodsIgnore number of parameters for methods with @Override annotationtrue

[8]ページ先頭

©2009-2025 Movatter.jp