Chapter 3: Code of bad taste

"If the diapers stink, we replace it."

--Beck grandmother, on the philosophy of keeping children clean


Bad taste of this chapter concentrates on the code when the reconstruction. Specific methods of reconstruction in the following sections.

"There is no comparable measure rules intuition extensive experience those. You have to develop their own judgment, learn to judge how much of a class instance variables considered too large a number of functions within the code was not too long."

​ ——Martin Flower

3.1 renamed

Thought to functions, modules, variables and class name, it clearly shows their features and usage.

One reconstruction techniques: rename (change the function declaration, renamed variables, fields renamed)

3.2 eliminate duplicate code - extraction function

Two functions of the same class containing the same expression - refining function.

Repeating code segment in the same different subclasses of a superclass - Move function.

3.3 Split long function

"Live the longest, the best procedure is often relatively short."

"The longer function, the more difficult to understand."

3.4 parameter list is too long

  • If several parameters are often found to occur by introducing a parameter object can be merged as one object. (Package)
  • If it is found out a lot of data items from existing data structure as a parameter, as a direct transfer full object
  • To check substitution parameters: You can get the value of another parameter query as a parameter.

3.5 Global Variables

Problem with global variables: anywhere in the code base can be modified, and can not be detected. (Code viruses)

Processing method: a package variables. Together with the function package, and then moved in the class or module, to control their access.

3.6 divergent changes and modifications shotgun style

Radiant variations: a fixed part of the code to modify a change is encountered.

Shotgun-style modification: the code of one of the bad taste, encounter changes need to modify many places.

Reducing the coupling module, the module independence. Achieve independent code changes when adding modification.

3.7 Attachment plot

Modular: to maximize the area of ​​internal interactions, minimizing inter-regional interaction.

Attachment plot: data exchange function or a function and another module is particularly frequent, far better than in their own internal exchange.

3.8 Data mud pie

"In the same field two classes, many of the same function signature parameters ...."

Refining class if necessary. Introduce Parameter Object, remain intact so that the object parameter list is shorter.

And weigh semantic form.

3.9 polymorphic replacement switch

3.10 Loops

To replace the pipe loop.

3.11 verbose elements

With the reconstruction of the class is getting smaller, timely went solemn meaning.

3.12 rhetoric versatility

If not, it is not worth it. No access to the device will only get in the way.

3.13 middleman

Packaging is often accompanied by the delegate.

Commission should be moderate, excessive commission will become redundant.

3.14 oversized classes

Resulting in duplication of code.

Class refining, refining superclass.

Notes 3.15

"When you feel the need to write a comment, please try to reconstruct."

Notes application scenarios:

  • This code did what
  • Record future plans
  • Why do

Guess you like

Origin www.cnblogs.com/noneplus/p/12049591.html