Everyone knows that "Magic Numbers" are bad, Bad, BAD! Take, for example, dividing something into quarters. Because including the literal "4" in your code would be a Magic Number, many developers agree that it's best to define a constant named "FOUR", and then use that constant instead. Makes sense, right?

Wrong! The problem with using "FOUR" is that, like "4", it's a Magic Constant. While not nearly as bad as Magic Numbers, Magic Constants certainly aren't elegant. Fortunately, Allen's colleague has provided us all with a valuable lesson the proper use of Magic and Non-Magic constants. Read it, and learn.

public class Constants {

    ...

    public static final int FOUR = 4;
    public static final int THREE = 3;

    public static final int INTEGER_FOUR = 4;
    public static final int INTEGER_FIVE = 5;
    public static final int INTEGER_ONE = 1;
    public static final int LENGTH_FOUR = 4;
    public static final int LENGTH_FIVE = 5;
    public static final int LENGTH_SEVEN = 7;
    public static final int LENGTH_EIGHT = 8;
    public static final int LENGTH_NINE = 9;
    public static final int LENGTH_ELEVEN = 11;
    public static final int LENGTH_TWELVE = 12;
    public static final int LENGTH_EIGHTEEN = 18;
    public static final int LENGTH_FIFTEEN = 15;
    public static final int ONE = 1;
    public static final int INTEGER_FIVE = 5;
    public static final int INTEGER_ONE = 1;
    public static final int PLUS_ONE = 1;
    public static final int INTEGER_THREE = 3;

    public static final Long LONG_VALUE_TEN = Long.valueOf(10);
    public static final Long LONG_VALUE_ZEARO = Long.valueOf(0);

    public static final BigDecimal BIGDECIMAL_ZERO = new BigDecimal(0);
    public static final BigDecimal BIGDECIMAL_ONE = new BigDecimal(1);

    public static final BigDecimal NEGATIVE_NUMBER_ONE = new BigDecimal(-1);

    public static final int COMPARE_RESULT_ZERO = 0 ;
    public static final int COMPARE_RESULT_ONE = 1 ;
    public static final int COMPARE_RESULT_NEGATIVE = -1 ;

    ...

    public static final String NINE_STRING = "9";
    public static final String ONE_STRING = "9";

}




[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!