1. [Mandatory] When using regex, precompile needs to be done in order to increase the matching performance.

Note: Do not define Pattern pattern = Pattern.compile(.); within method body.

2. [Mandatory] When using attributes of POJO in velocity, use attribute names directly. Velocity engine will invoke getXxx() of POJO automatically. In terms of boolean attributes, velocity engine will invoke isXxx() (Do not use is as prefix when naming boolean attributes).

Note: For wrapper class Boolean, velocity engine will invoke getXxx() first.

3. [Mandatory] Variables must add exclamatory mark when passing to velocity engine from backend, like \$!{var}.

Note: If attribute is null or does not exist, \${var} will be shown directly on web pages.

4. [Mandatory] The return type of Math.random() is double, value range is 0<=x<1 (0 is possible). If a random integer is required, do not multiply x by 10 then round the result. The correct way is to use nextInt or nextLong method which belong to Random Object.

5. [Mandatory] Use System.currentTimeMillis() to get the current millisecond. Do not use new Date().getTime().

Note: In order to get a more accurate time, use System.nanoTime(). In JDK8, use Instant class to deal with situations like time statistics.

6. [Recommended] It is better not to contain variable declaration, logical symbols or any complicated logic in velocity template files.

7. [Recommended] Size needs to be specified when initializing any data structure if possible, in order to avoid memory issues caused by unlimited growth.

8. [Recommended] Codes or configuration that is noticed to be obsoleted should be resolutely removed from projects.

Note: Remove obsoleted codes or configuration in time to avoid code redundancy.

Positive example: For codes which are temporarily removed and likely to be reused, use /// to add a reasonable note.

  1. public static void hello() {
  2. /// Business is stopped temporarily by the owner.
  3. // Business business = new Business();
  4. // business.active();
  5. System.out.println("it's finished");
  6. }