Specification first-(two) constant definition

  1. [] Does not allow any magic force value (i.e. non-predefined constants) appear directly in the code
    counter example:String key = "Id#taobao_" + tradeId;

  2. [Mandatory] When assigning values ​​to long or Long, use uppercase L after the value, not lowercase l, lowercase is easy to be confused with the number 1, causing misunderstanding

  3. [Recommended] Do not use one constant class to maintain all constants, but classify according to the constant function and maintain them separately.
    Note: The large and complete constant classes are messy and unstructured. Use the search function to locate the modified constant, which is not conducive to understanding and maintaining the
    positive : Cache related constants are placed under the class CacheConsts; system configuration related constants are placed under the class ConfigConsts

  4. [Recommended] There are five levels of constant reuse: constants shared across applications, constants shared within applications, constants shared within subprojects, constants shared within packages, and constants shared within classes

  • Sharing constants across applications: placed in a two-party library, usually under the constant directory in client.jar
  • In-app shared constants: placed in a library, usually under the constant directory in the submodule
    Counter-example: easy-to-understand variables must also be uniformly defined as in-app shared constants, the two siege divisions define the expressions in the two classes respectively. "Yes" variables:
    Class A: public static final String YES = "yes";
    Class B: public static final String YES = "y";
    A.YES.equals (B.YES), expected to be true, but actual Returns false, causing online problems
  • Constants shared within subprojects: that is, under the constant directory of the current subproject
  • Constants shared within the package: ie under a separate constant directory under the current package
  • Constants shared within the class: private static final definitions directly within the class
  1. [Recommended] If the variable value only changes within a fixed range, use the enum type to define the
    description: If there is an extension attribute other than the name, the enum type should be used. The number in the positive example below is the extension information, indicating the number of the year Seasons
public enum SeasonEnum {
     SPRING(1), SUMMER(2), AUTUMN(3), WINTER(4);
     private int seq;
     SeasonEnum(int seq){
         this.seq = seq;
     }
}

Guess you like

Origin www.cnblogs.com/StivenYang/p/12716043.html