DRY (Don't Repeat Yourself) Rules ("rulesets/dry.xml")

These rules check for duplicate code, enforcing the DRY (Don't Repeat Yourself) principle.

DuplicateNumberLiteral Rule

Since CodeNarc 0.11

This rule checks for duplicate number literals within the current class.

Code containing duplicate Number literals can usually be improved by declaring the Number as a constant field.

By default, the rule does not analyze test files. This rule sets the default value of the doNotApplyToClassNames property to only match class names that do not end in 'Test', 'Tests' or 'TestCase'.

Property Description Default Value
ignoreNumbers The optional comma-separated list of numbers that should be
ignored (i.e., not cause a violation).
0,1

Notes

  • This rule does not search across several files at once, only in the current file, and only within the current class.
  • You can suppress the error by annotating a class or method with the @SuppressWarnings('DuplicateLiteral') annotation.

DuplicateStringLiteral Rule

Since CodeNarc 0.11

This rule checks for duplicate String literals within the current class.

Code containing duplicate String literals can usually be improved by declaring the String as a constant field.

By default, the rule does not analyze test files. This rule sets the default value of the doNotApplyToClassNames property to only match class names that do not end in 'Test', 'Tests' or 'TestCase'.

Property Description Default Value
ignoreStrings The optional comma-separated list of Strings that should be
ignored (i.e., not cause a violation).
'' (empty string)

Notes

  • This rule does not search across several files at once, only in the current file, and only within the current class.
  • You can suppress the error by annotating a class or method with the @SuppressWarnings('DuplicateLiteral') annotation.