"Alibaba Development Manual" reading notes (4)

7.1 1. [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

Put it last, even if it has no code.


7.2 2. [Mandatory] Braces must be used in if / else / for / while / do statements, even if there is only one line of code, avoid using the following form: if (condition) statements;


7.3 3. [Recommendation] It is recommended to use the else as little as possible. The if - else method can be rewritten as:
if(condition){
    ...
    return obj;
}
// Then write the business logic code of the else ; Note: If you have to use if( )...else if()...else... way to express logic, [mandatory] Do not exceed 3 layers, and use the state design pattern.


Positive example: Logically more than 3 levels of if-else code can be implemented using guard statements or state patterns.


7.5 5. [Recommended] The performance of the statements in the loop body should be considered. The following operations should be moved to the outside of the loop as much as possible, such as defining objects, variables,
obtaining database connections, and performing unnecessary
try - catch operations ( whether this try - catch can be moved ? out of the circulation ) .


7.6 6. [Recommended] Interface input parameter protection. This scenario is commonly used for interfaces used for batch operations.


7.7 7. [Reference] Scenarios that require parameter verification in methods:
1 ) Methods with low calling frequency.
2 ) For methods with high execution time overhead, the parameter verification time is almost negligible, but if the
intermediate execution rolls back due to parameter errors, or errors are made, it will not be worth the loss.

Alibaba Java Development Manual
- Prohibited for commercial use, offenders must be investigated - 16 / 34
3 ) Methods that require extremely high stability and usability.
4 ) Open interface provided externally, whether it is RPC / API / HTTP interface.
5 ) Sensitive permission entry.


7.8 8. [Reference] Scenarios that do not require parameter verification in methods:
1 ) For methods that are likely to be called cyclically, it is not recommended to verify parameters. However, the external
parameter .
2 ) The underlying methods are called frequently, and are generally not verified. After all, it is like the last step of pure water filtration, and parameter
errors are unlikely to expose problems at the bottom. Generally , the
DAO layer and the Service layer are in the same application and deployed in the same
server, so the parameter verification of
DAO can be omitted.
3 ) A method declared as private can only be called by its own code. If it can be determined that the incoming
parameters have been checked or there is no problem, the parameters can be omitted at this time.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324726178&siteId=291194637