The most simple java code specifications summary

Before recently participated in a 'clean way of code' training organized by the company, feel benefit, which refers to a principle quite meaningless code specifications, referred to as 'the simplest code specifications', especially in this specification to help novice to comply these simple specifications can rapidly improve code quality.

As follows (following the '=' can be understood as: 'write' or 'to optimize'):

 

1. variable naming

- noun class name =

- method name = verb / verb-object phrase

- Boolean type variable = is + adjective / is + past participle verb

- common variable = singular nouns

- a collection of variables = n.

- map = key2Value format

- variable name = name written in variable types

 

2. Elimination of constants

- Ordinary been defined constants constant =

- type of limited value = enumeration constants

- class name / name of the global variable / method name = reflection (I understand: reflection performance overhead is relatively large, but for most applications is acceptable, using reflection and annotation can greatly reduce the amount of code, eliminate duplicate code )

 

3. eliminate else / case branch

- Dual Branch = delete and return to (my understanding: for example if..else structure, you can use only one if, at the end if return, else contents outside if, this code is easy to understand, better readability)

- = triplet of expressions to simple branch

- data conversion = map / dictionary (I understand, similar to if (i == 1) ... Code if (i == 2), the corresponding relationship can be represented by the structure similar to the map, the map is 1 and key 2 , value of some values ​​if the required)

- = generic types / templates (generic is a good mechanism that can adapt to different parameters / return type, to reduce duplication of code)

- = flow is the same process details different abstract class / method (a method to extract a common process used, different methods written abstract method, is commonly used code optimization method)

- Process = different polymorphic (my understanding is that the extraction interface, and then implement different processes)

 

4. The method of eliminating long

- Segmentation Sequence structure =

- = elongate structure abstract methods

- Elimination of branch multi-branch structure =

 

The loop for eliminating double (or even for a multilayer loop)

- two double for loop = 1: n objects (two objects using 1: n relationship of the object to eliminate multiple loop, instead of the original multi-cycle write in an object / class inside)

 

6. Elimination of cycling cursor and iterator (recommended foreach)

- for in i = foreach

- iterator = foreach

 

Guess you like

Origin www.cnblogs.com/xhj123/p/12602306.html