1. [Mandatory] In a switch block, each case should be finished by break/return. If not, a note should be included to describe at which case it will stop. Within every switch block, a default statement must be present, even if it is empty.

    2. [Mandatory] Braces are used with if, else, for, do and while statements, even if the body contains only a single statement. Avoid using the following example:

    1. if (condition) statements;

    3. [Recommended] Use else as less as possible, if-else statements could be replaced by:

    1. if (condition) {
    2. ...
    3. return obj;
    4. }
    5. // other logic codes in else could be moved here

    Note: If statements like if()...else if()...else... have to be used to express the logic, [Mandatory] nested conditional level should not be more than three.

    Positive example: if-else code with over three nested conditional levels can be replaced by guard statements or State Design Pattern. Example of guard statement:

    1. public void today() {
    2. if (isBusy()) {
    3. System.out.println("Change time.");
    4. return;
    5. }
    6. if (isFree()) {
    7. System.out.println("Go to travel.");
    8. return;
    9. }
    10. System.out.println("Stay at home to learn Alibaba Java Coding Guidelines.");
    11. return;
    12. }

    4. [Recommended] Do not use complicated statements in conditional statements (except for frequently used methods like getXxx/isXxx). Use boolean variables to store results of complicated statements temporarily will increase the code’s readability.

    Note: Logic within many if statements are very complicated. Readers need to analyze the final results of the conditional expression to decide what statement will be executed in certain conditions.

    Positive example:

    1. // please refer to the pseudo-code as follows
    2. boolean existed = (file.open(fileName, "w") != null) && (...) || (...);
    3. if (existed) {
    4. ...
    5. }

    Counter example:

    1. if ((file.open(fileName, "w") != null) && (...) || (...)) {
    2. ...
    3. }

    5. [Recommended] Performance should be considered when loop statements are used. The following operations are better to be processed outside the loop statement, including object and variable declaration, database connection, try-catch statements.

    6. [Recommended] Size of input parameters should be checked, especially for batch operations.

    7. [For Reference] Input parameters should be checked in following scenarios:
      1) Low-frequency implemented methods.
      2) Overhead of parameter checking could be ignored in long-time execution methods, but if illegal parameters lead to exception, the loss outweighs the gain. Therefore, parameter checking is still recommended in long-time execution methods.
      3) Methods that needs extremely high stability or availability.
      4) Open API methods, including RPC/API/HTTP.
      5) Authority related methods.

    8. [For Reference] Cases that input parameters do not require validation:
      1) Methods very likely to be implemented in loops. A note should be included informing that parameter check should be done externally.
      2) Methods in bottom layers are very frequently called so generally do not need to be checked. e.g. If DAO layer and Service layer is deployed in the same server, parameter check in DAO layer can be omitted.
      3) Private methods that can only be implemented internally, if all parameters are checked or manageable.