Statute of the design method Java

Rule 1. length measurement method [Recommended]

Methods try not to exceed the number of lines 100 lines, or other teams mutually agreed.

Further, when the method of code length exceeds 8000 bytes, it will not be compiled into binary code JIT.

  • Sonar-107: Methods should not have too many lines, default value to 100

  • Facebook-Contrib:Performance - This method is too long to be compiled by the JIT


Rule 2. [Recommended] method statement in the same abstraction level

Counterexample: a method where, during the first 20 lines of code is very complicated to calculate the basic price, and then call a discount calculation function, and then call a function to calculate premiums.

At this time, the first 20 rows can be packaged into a price calculation function, so that the whole method on the same level of abstraction.


Rule 3. [Recommended] To assist the reader, and inline method, exception handling and the other a very small probability of entering the small probability of occurrence code path, encapsulated into a separate method

if(seldomHappenCase) {
  hanldMethod();
}
try {
  ...
} catch(SeldomHappenException e) {
  handleException();
}

Rule 4. [Recommended] minimize duplication code extraction method

More than 5 lines duplicate code, can be considered common extraction methods.


Rule 5. [Method] recommended parameter is preferably not more than 3, no more than 7 up

1) If a plurality of objects belonging to the same parameters, passing objects.

Exception: You do not want to rely on the whole object, spread dependencies between classes.

2) a plurality of parameters into a single logical object is newly created.

Exception: no logical association between a plurality of parameters.

3) The function is split into a plurality of functions, each of the parameters so that the desired function is reduced.

  • Sonar-107: Methods should not have too many parameters


Rule 6. [Recommended] the following circumstances, the need for calibration parameters

1) call the low frequency method.

2) to perform a lot of time overhead method. In this case, the parameter calibration time is almost negligible, but if because of parameter error led to the middle of a rollback or error, more costly.

3) high stability and availability of the required methods.

4) open interface provided externally, whether RPC / HTTP / public library API interface.

If you use Apache Validate or Guava Precondition for verification, and additional error messages, be careful not to have to do a check every string concatenation.

//WRONG
Validate.isTrue(length > 2, "length is "+keys.length+", less than 2", length);
//RIGHT
Validate.isTrue(length > 2, "length is %d, less than 2", length);

Rule 7. [Recommended] the following circumstances, does not require calibration parameters

1) most likely method is called a cycle.

2) a relatively high frequency level calls a method. After all, it is like pure water filtration final, parameter error will be less likely to be exposed to the underlying problem.

For example, generally DAO layer Service layer in the same application, the calibration parameter DAO layer, may be omitted.

3) be declared as private, or other means will only be called by their own code can determine if the caller has to do an inspection, or certainly not a problem can be omitted.

Even ignoring inspection, but also try to explain the method specified in the parameters of the requirements, such as vjkit the @ NotNull, @ Nullable logo.


Rule 8. [Recommended] check parameters do assert disabled

assert assert only used to verify a test environment to debug, without in the production environment. Because it is necessary to increase -ea startup parameters it will be executed. And check fails will throw a AssertionError (belonging Error, need to capture Throwable)

Thus in the verification production environment, it is necessary to use the Validate Precondition Apache Commons Lang or of Guava.


Rule 9. [Recommended] Return Value may Null, consider Optional use of Class JDK8

Not forced to return empty set, or a null object. But need to add a comment to fully explain under what circumstances would return a null value.

This manual is clear 防止NPE是调用者的责任. Even if the called method returns an empty collection or an empty object to the caller, is not sit back and relax, we must take into account the remote call fails, the sequence of failure, abnormal returns null, the scene of running.

Use Optional classes JDK8 not unfold here.


Rule 10. [Recommended] Return Value may be set for the internal array and

If that is unlikely to be modified outside, or no effect, not mandatory before returning Immutable wrapped into a set or an array of clones.


Rule 11. [Recommended] can not use the argument types of inheritance to override method

Because the parameter type is the actual type of method overloading matched runtime compiling the surface according to the type of matching is not in accordance.

class A {
  void hello(List list);
  void hello(ArrayList arrayList);
}
List arrayList = new ArrayList();
// 下句调用的是hello(List list),因为arrayList的定义类型是List
a.hello(arrayList);  

Rule 12. [mandatory] interfaces, are not allowed to modify the method signature being called outside to avoid an impact on the caller interface

Only add a new interface, and obsolete interfaces plus @Deprecated comment, and clearly explain what new interfaces Yes.


Rule 13. [Recommended] do not use @Deprecatedthe class or method

Interface provider is outdated since it clear interface and provides a new interface, then as the caller, the obligation to implement any new research outdated methods of yes.

For example, the method decode (String encodeStr) java.net.URLDecoder of this method is outdated, you should use two-parameter decode (String source, String encode).


[Recommended] Rule 14. The method without using unstable, hydrocarbons such as * packages under the class library under the bottom com.sun internal packet

com.sun.*, sun.*Packages under the class, or the class name of the class library bottom under internal package, is not exposed outside, can be changed at any time by the unstable class.

  • Sonar-1191: Classes from "sun.*" packages should not be used

Micro-channel public number 

 


Guess you like

Origin www.cnblogs.com/niugang0920/p/12359028.html