control statement
Other related articles
Java Coding Specification 1 (Programming Specification - Naming Style)
Java Coding Specification 2 (Programming Specification - Constant Definition)
Java Coding Specification 3 (Programming Specification - Code Format)
Java Coding Specification 4 (Programming Specification - OOP Specification)
Java Coding Specification 5 (Programming Specification - Collection Processing)
Java Coding Specification 6 (Programming Specification - Concurrent Processing)
Java Coding Specification 7 (Programming Specification - Control Statement)
Java Coding Specification 8 (Programming Specification - Annotation Specification and Others)
Java Coding Specification 9 (Exception Log )
Java Coding Specification 10 (Unit Test)
Java Coding Specification 11 (Security Specification)
Java Coding Specification 12 (MySQL-Table Building Specification)
Java Coding Specification 13 (MySQL-Index Specification)
Java Coding Specification 14 (MySQL-SQL Statement and ORM Mapping )
Java Coding Specification 15 (Project Structure)
[Mandatory] In a switch block, each case is either terminated by break/return, etc., or a comment indicates which case the program will continue to execute; in a switch block, a default statement must be included and placed at the end , even with empty code.
[Mandatory] Braces must be used in if/else/for/while/do statements. Even if there is only one line of code, avoid single-line coding:
if (condition) statements;
[Mandatory] In high concurrency scenarios, avoid using "equals" as a condition for mid or exit.
- Note: If the concurrency control is not handled properly, the "breakdown" condition is replaced by the greater or lesser interval judgment condition.
- Counter example: When it is judged that the number of remaining prizes is equal to 0, the distribution of prizes will be terminated, but due to a parallel processing error, the number will instantly become a negative number, so the event cannot be terminated.
[Recommended] When expressing abnormal branches, use the if-else method less.
Recommended way:
if (condition) { ... return obj; } // 接着写else的业务逻辑代码;
- Note: If you have to use if()…else if()…else… to express logic, [mandatory] To avoid the difficulty of subsequent code maintenance, please do not exceed 3 layers.
Positive example: The logic judgment code of if-else with more than 3 layers can be implemented by using guard statements, strategy patterns, state patterns, etc. The examples of guard statements are as follows:
public void today() { if (isBusy()) { System.out.println(“change time.”); return; } if (isFree()) { System.out.println(“go to travel.”); return; } // 也是皮,只有不忙不闲的时候来学习 System.out.println(“stay at home to learn Alibaba Java Coding Guidelines.”); return; }
[Recommended] In addition to common methods (such as getXxx/isXxx), do not execute other complex statements in conditional judgments, and assign the result of complex logical judgments to a meaningful Boolean variable name to improve readability.