MagicNumber

Since Checkstyle 3.1

Description

Checks that there are no "magic numbers" where a magic number is a numeric literal that is not defined as a constant. By default, -1, 0, 1, and 2 are not considered to be magic numbers.

Constant definition is any variable/field that has 'final' modifier. It is fine to have one constant defining multiple numeric literals within one expression:

static final int SECONDS_PER_DAY = 24 * 60 * 60;
static final double SPECIAL_RATIO = 4.0 / 3.0;
static final double SPECIAL_SUM = 1 + Math.E;
static final double SPECIAL_DIFFERENCE = 4 - Math.PI;
static final Border STANDARD_BORDER = BorderFactory.createEmptyBorder(3, 3, 3, 3);
static final Integer ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE = new Integer(42);
        

Properties

name description type default value since
constantWaiverParentToken Specify tokens that are allowed in the AST path from the number literal to the enclosing constant definition. subset of tokens TokenTypes ARRAY_INIT , ASSIGN , BAND , BNOT , BOR , BSR , BXOR , COLON , DIV , ELIST , EQUAL , EXPR , GE , GT , LE , LITERAL_NEW , LT , METHOD_CALL , MINUS , MOD , NOT_EQUAL , PLUS , QUESTION , SL , SR , STAR , TYPECAST , UNARY_MINUS , UNARY_PLUS 6.11
ignoreAnnotation Ignore magic numbers in annotation declarations. boolean false 5.4
ignoreAnnotationElementDefaults Ignore magic numbers in annotation elements defaults. boolean true 8.23
ignoreFieldDeclaration Ignore magic numbers in field declarations. boolean false 6.6
ignoreHashCodeMethod Ignore magic numbers in hashCode methods. boolean false 5.3
ignoreNumbers Specify non-magic numbers. double[] -1, 0, 1, 2 3.1
tokens tokens to check subset of tokens NUM_DOUBLE , NUM_FLOAT , NUM_INT , NUM_LONG . NUM_DOUBLE , NUM_FLOAT , NUM_INT , NUM_LONG . 3.1

Examples

To configure the check with default configuration:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MagicNumber"/>
  </module>
</module>
        

results in the following violations:

@Example1.Annotation(6) // violation, ''6' is a magic number.'
public class Example1 {
  private int field = 7; // violation, ''7' is a magic number.'

  void method1() {
    int i = 1;
    int j = 8; // violation, ''8' is a magic number.'
  }
  public void method2() {
    final TestClass testObject = new TestClass(62);
    final int a = 3;
    final int[] b = {4, 5};
    final int c = -3;
    final int d = +4;
    final int e = method3(10, 20);
    final int f = 3 * 4;
    final int g = 3 / 4;
    final int h = 3 + 4;
    final int i = 3 - 4;
    final int j = (int) 3.4;
  }
  private int method3(int a, int b) {
    return a + b;
  }
  public int hashCode() {
    return 10; // violation, ''10' is a magic number.'
  }
  @interface Annotation {
    int value() default 10;
    int[] value2() default {10};
  }
  class TestClass {
    TestClass(int field) {}
  }
}
        

To configure the check so that it checks floating-point numbers that are not 0, 0.5, or 1:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MagicNumber">
      <property name="tokens" value="NUM_DOUBLE, NUM_FLOAT"/>
      <property name="ignoreNumbers" value="0, 0.5, 1"/>
      <property name="ignoreFieldDeclaration" value="true"/>
      <property name="ignoreAnnotation" value="true"/>
    </module>
  </module>
</module>
        

results in no violations:

@Example2.Annotation(6)
public class Example2 {
  private int field = 7;

  void method1() {
    int i = 1;
    int j = 8;
  }
  public void method2() {
    final TestClass testObject = new TestClass(62);
    final int a = 3;
    final int[] b = {4, 5};
    final int c = -3;
    final int d = +4;
    final int e = method3(10, 20);
    final int f = 3 * 4;
    final int g = 3 / 4;
    final int h = 3 + 4;
    final int i = 3 - 4;
    final int j = (int) 3.4;
  }
  private int method3(int a, int b) {
    return a + b;
  }
  public int hashCode() {
    return 10;
  }
  @interface Annotation {
    int value() default 10;
    int[] value2() default {10};
  }
  class TestClass {
    TestClass(int field) {}
  }
}
        

To configure the check so that it ignores magic numbers in field declarations:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MagicNumber">
        <property name="ignoreFieldDeclaration" value="true"/>
    </module>
  </module>
</module>
        

results in the following violations:

@Example3.Annotation(6) // violation, ''6' is a magic number.'
public class Example3 {
  private int field = 7;

  void method1() {
    int i = 1;
    int j = 8; // violation, ''8' is a magic number.'
  }
  public void method2() {
    final TestClass testObject = new TestClass(62);
    final int a = 3;
    final int[] b = {4, 5};
    final int c = -3;
    final int d = +4;
    final int e = method3(10, 20);
    final int f = 3 * 4;
    final int g = 3 / 4;
    final int h = 3 + 4;
    final int i = 3 - 4;
    final int j = (int) 3.4;
  }
  private int method3(int a, int b) {
    return a + b;
  }
  public int hashCode() {
    return 10; // violation, ''10' is a magic number.'
  }
  @interface Annotation {
    int value() default 10;
    int[] value2() default {10};
  }
  class TestClass {
    TestClass(int field) {}
  }
}
        

To configure the check to check annotation element defaults:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MagicNumber">
      <property name="ignoreAnnotationElementDefaults" value="false"/>
    </module>
  </module>
</module>
        

results in the following violations:

@Example4.Annotation(6) // violation, ''6' is a magic number.'
public class Example4 {
  private int field = 7; // violation, ''7' is a magic number.'

  void method1() {
    int i = 1;
    int j = 8; // violation, ''8' is a magic number.'
  }
  public void method2() {
    final TestClass testObject = new TestClass(62);
    final int a = 3;
    final int[] b = {4, 5};
    final int c = -3;
    final int d = +4;
    final int e = method3(10, 20);
    final int f = 3 * 4;
    final int g = 3 / 4;
    final int h = 3 + 4;
    final int i = 3 - 4;
    final int j = (int) 3.4;
  }
  private int method3(int a, int b) {
    return a + b;
  }
  public int hashCode() {
    return 10; // violation, ''10' is a magic number.'
  }
  @interface Annotation {
    int value() default 10; // violation, ''10' is a magic number.'
    int[] value2() default {10}; // violation, ''10' is a magic number.'
  }
  class TestClass {
    TestClass(int field) {}
  }
}
        

Config example of constantWaiverParentToken option:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MagicNumber">
      <property name="constantWaiverParentToken" value="ASSIGN, ARRAY_INIT, EXPR,
      UNARY_PLUS, UNARY_MINUS, TYPECAST, ELIST, DIV, PLUS"/>
    </module>
  </module>
</module>
        

results in the following violations:

@Example5.Annotation(6) // violation, ''6' is a magic number.'
public class Example5 {
  private int field = 7; // violation, ''7' is a magic number.'

  void method1() {
    int i = 1;
    int j = 8; // violation, ''8' is a magic number.'
  }
  public void method2() {
    final TestClass testObject = new TestClass(62);
    // violation above, ''62' is a magic number.'
    final int a = 3; // ok as waiver is ASSIGN
    final int[] b = {4, 5}; // ok as waiver is ARRAY_INIT
    final int c = -3; // ok as waiver is UNARY_MINUS
    final int d = +4; // ok as waiver is UNARY_PLUS
    final int e = method3(10, 20);
    // 2 violations above:
    //  ''10' is a magic number.'
    //  ''20' is a magic number.'
    final int f = 3 * 4;
    // 2 violations above:
    //  ''3' is a magic number.'
    //  ''4' is a magic number.'
    final int g = 3 / 4; // ok as waiver is DIV
    final int h = 3 + 4; // ok as waiver is PLUS
    final int i = 3 - 4;
    // 2 violations above:
    //  ''3' is a magic number.'
    //  ''4' is a magic number.'
    final int j = (int) 3.4; //ok as waiver is TYPECAST
  }
  private int method3(int a, int b) {
    return a + b;
  }
  public int hashCode() {
    return 10; // violation, ''10' is a magic number.'
  }
  @interface Annotation {
    int value() default 10;
    int[] value2() default {10};
  }
  class TestClass {
    TestClass(int field) {}
  }
}
        

Config example of ignoreHashCodeMethod option:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MagicNumber">
        <property name="ignoreHashCodeMethod" value="true"/>
    </module>
  </module>
</module>
        

results in the following violations:

@Example6.Annotation(6) // violation, ''6' is a magic number.'
public class Example6 {
  private int field = 7; // violation, ''7' is a magic number.'

  void method1() {
    int i = 1;
    int j = 8; // violation, ''8' is a magic number.'
  }
  public void method2() {
    final TestClass testObject = new TestClass(62);
    final int a = 3;
    final int[] b = {4, 5};
    final int c = -3;
    final int d = +4;
    final int e = method3(10, 20);
    final int f = 3 * 4;
    final int g = 3 / 4;
    final int h = 3 + 4;
    final int i = 3 - 4;
    final int j = (int) 3.4;
  }
  private int method3(int a, int b) {
    return a + b;
  }
  public int hashCode() {
    return 10;
  }
  @interface Annotation {
    int value() default 10;
    int[] value2() default {10};
  }
  class TestClass {
    TestClass(int field) {}
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker