Code bad taste - reconstruction to improve the design of existing code

3.1 duplicate code (if you need to modify the sections of the code, you need to change a lot of places to be causing problems. Difficult to maintain.)

If the two functions of a class have the same expression, the expression can be refined into function, two places are calling this function.

If there are two subclasses of the same expression, refined into an expression function, class push trade deficit. If the code only Similarly, when the need to refine the function similar parts and portions difference divided to constitute a single function. This may create a template function or can use this template method design pattern.

If some functions do the same thing in a different algorithm, you can choose a clearer, replacement algorithm to replace it.

If two unrelated classes duplication of code that can be refined into consideration for one of the new class, another class to call this new class.

3.2 function is too long (too long function general description of functions do too many things contrary to the principle of single responsibility would break down into finer-grained function function, easy-to-use and transformation.)

Whenever feel the need to comment to explain something, we need to put things written description of a separate function, and named by its use. (The code more difficult to understand when)

Ninety-nine percent of the cases, the function should become smaller, just use the find function for refining function together parts (except refining function, also we need to consider the location of the storage function)

If the new function to extract a function of memory in a large number of temporary variables and parameters, can be eliminated by replacing the temporary elements using temporary variables queries (if the query does not spend time and resources, in general, create temporary variables need to allocate additional address, create too much temporary variables will waste a lot of system resources, replace the temporary variable with the query would save address space. Of course, not to say totally unusable temporary variables, if the use of temporary variables can easily understand the code can also be used, but with caution. too many temporary variables may cause problem). Can also be used to introduce parameter object or objects remain intact too long to make some of the parameters of the column becomes simple.

If you do the above operation or there are too many temporary variables and parameters, you should use the function object replace function.

Those parts need to refine

First, look for notes, annotated place a general description of the place is not well understood. And the reason is not well understood is likely to do something extra. This requires extracted

Second, conditional expressions and loops. The conditional expression may be treated by decomposing expression. And code in the circulating loop required refined into function.

3.3 oversized class (if the class needs to be done a lot, prone to a lot of instance variables, then immediately there will be a lot of duplicated code)

Several variables to refine the new class, refined much as possible when selecting the relevant variables.

If a class has a lot of code, often suitable for refining class or subclass refinement. Here are tips, first determine how the client uses, then use the interface to extract every way to extract an interface.

3.4 The parameter list is too long (too many parameters, the function uses a high cost, because without the required parameter function can not call a function)

If the argument mostly from an object, you can direct incoming object. Get parameters from the object. If the request can be sent to the object can be acquired by the parameter, then the parameter function should be replaced. Use keeping intact the object of the same object from a bunch of data collected, replacing them with the object. If the lack of a reasonable target data ownership, you can use a reference parameter object to create a "parameter object" as they

3.5 divergent variation (be split into a plurality of classes, each process corresponding to the change)

For all the appropriate modifications certain external changes should take place in a single class. All contents of the new class should reflect this change. All changes should identify a specific cause, and then use it to extract class to refine the new class.

3.6 shotgun-style modifications (changes will unify to the same place, unified modify, easy maintenance)

And divergent changes the contrary, if you encounter a change, we need to modify the code is distributed in four, difficult to modify, and easy to forget. And moving functions should be used to move all modifications field code into the same class, the class would not create a proper. We can use the inside of the class with a series of acts into the same category.

Refers to a class divergent changes affected by various changes, modifications refers to a shotgun type change induced modifications plurality of classes.

3.7 Attachment Complex (the relevant class and function are put together, easier to manage)

Function interest in a class higher than the class itself is located, a function call most of the other classes of objects. The functions required to move to this class. If only a part, then distilling it into a function and then move.

It determines which class has the most data used by this function, and this function into the class

3.8 Data mud pie (there are dependencies between data, create new objects easier to manage)

In the same field two classes, many function signatures have the same parameters, these data are always tied together should have their own object.

Delete a large number of data, other data have not lost significance, if no longer makes sense, then it should generate a new object for them. (Description of the dependent relationship between data)

3.9 The basic type paranoid (not paranoid to use the base type, sometimes you need to create new data types)

If there is a group of fields should always be placed together, the use of refined class. If the basic parameters of the data type column, parameter object references may be used, or alternatively using an array of objects.

3.10 switch statement (polymorphic replacement switch)

Using a switch statement extract function to extract an independent function, the function will move to the required class polymorphism.

3.11 parallel inheritance system

Every time you add a subclass to a class, we must also add a subclass other classes. Let other instances of the class reference to an instance of another inheritance hierarchy.

3.12 verbose class

3.13 rhetoric Futurity

3.14 confusing temporary field

3.15 over message link coupling

3.16 middleman

3.17 Xiani relations

3.18 similar to class

3.19 imperfect library

3.20 Data Class

3.21 bequeathed rejected

3.22 Excessive comment

 

Guess you like

Origin www.cnblogs.com/lgh344902118/p/11220718.html