【Code Specifications】Common Coding Specifications

Article source: Official Account-Intelligent IT System.


1. Clarify the function of the method and implement the method design precisely (rather than approximately). If a function will be implemented in multiple places, even if it is only two lines of code, you should write the method implementation.

illustrate:

Although it seems unnecessary to compile methods for functions that can be completed with only one or two lines, using methods can clarify functions, increase program readability, and facilitate maintenance and testing.

 

2. It should be clearly stipulated that the validity check of the parameters of the interface method should be the responsibility of the caller of the method or the responsibility of the interface method itself. The default is the responsibility of the method caller.

illustrate:

There are often two extreme phenomena for the validity check of the parameters of the interface method between modules, that is, either the caller and the callee do not check the validity of the parameters, and as a result, the necessary validity check is omitted. The processing process will cause hidden problems; or both the caller and the callee will check the validity of the parameters. Although this situation will not cause problems, it will generate redundant code and reduce efficiency.

 

3. Clarify the function of the class, and implement the design of the class precisely (rather than approximately). A class implements only a similar set of functionality. Note: When dividing classes, you should try to separate logical processing, data and display to achieve the unity of class functions.

Example:

Data classes cannot contain logic for data processing. Communication classes cannot contain logic for explicit processing.

 

4. All data classes must overload the toString()  method to return meaningful content of the class. Note: If the parent class implements a reasonable toString()  , the subclass can inherit it without rewriting it.

Example:

public TopoNode
 {      private String nodeName;     public String toString()      {          return "NodeName : " +  nodeName;      } }





 

5. Objects that need to end close() in database operations, IO operations, etc. must be close ( ) in the finally of try-catch-finally  .


6. After the exception is caught, if the exception is not handled, the log should be recorded (for the background).

Explanation: If there is a special reason, it must be explained with a note.


7. The exception thrown by yourself must fill in the detailed description information.

Description: It is convenient to locate the problem.

Example:

throw  new IOException("Writing data error! Data: " + data.toString());

 

8.  Whether to use exception handling or error return code handling in the program is determined according to whether it is beneficial to the program structure, and exceptions and error codes should not be mixed. It is recommended to use exceptions. Description: A system or module should uniformly plan the meaning of exception types and return codes. However, exceptions cannot be used for general process processing. Do not use exceptions too much. The processing efficiency of exceptions is lower than that of conditional branches, and the jumping process of exceptions is difficult to predict.

 

9.避免使用不易理解的数字,用有意义的标识来替代。涉及物理状态或者含有物理意义的常量,不应直接使用数字,必须用有意义的静态变量来代替。
示例:

如下的程序可读性差

if  (state == 0)

{

state = 1;

... // program  code

}

应改为如下形式

private final static int  TRUNK_IDLE = 0;
 private final static int TRUNK_BUSY = 1;
 private final static int TRUNK_UNKNOWN = -1;
 
 

if (state == TRUNK_IDLE)

{
     state = TRUNK_BUSY;
     ... // program code

}

 

10.数组声明的时候使用 int[] index ,而不要使用 int index[] 。说明:

 

11.异常捕获尽量不要直接 catch (Exception ex) ,应该把异常细分处理。


12.不要使用难懂的技巧性很高的语句,除非很有必要时。说明:高技巧语句不等于高效率的程序,实际上程序的效率关键在于算法。


公众号-智能化IT系统。每周都有技术文章推送,包括原创技术干货,以及技术工作的心得分享。扫描下方关注。



公众号-智能化IT系统。每周都有技术文章推送,包括原创技术干货,以及技术工作的心得分享。扫描下方关注。

Guess you like

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