Learn reconstruction

What is remodeling?

"Reconstruction" of the term presumably familiar to everyone, is the sort of code chant, but in fact not the case. It aims to reconstruct the premise does not change the behavior of the caller, the internal logic of the adjustment and optimization, to improve their understanding and reduce its modification costs.

When remodeling?

Reconstruction is not taking the time to focus on the individual, but when you want to do a feature, you should shut the place needs reconstruction reconstructed.

How reconstruction?

Abstract logical repeated

Repeat code is the most common core of early warning information, if there are two or more repeating logic code, it should be considered for consolidation. For example, the same class or different classes of methods exist if the same logic portion, it should be part of the same abstract as an independent method or class.

Long decomposition method

When a lot of people read others hundreds of lines of code or even thousands of lines, cast doubt on life. To facilitate understanding, the best way is to long method into several smaller method, coupled with easy to understand the method name, you can understand the code the same as natural language.

Reduction parameters

There is a very bad habit, is to use all the variables as parameters to the method, this will exacerbate the difficulty of understanding the code, expand the content is extremely difficult, when more data had to modify the parameters of all methods, led by a launch body. If the object as a parameter, the need to use data objects are put in, we can effectively solve the problem of parameters is too long.

Methods derailment

If you find that a certain class method calls frequently, it is likely to give you wearing a green hat, it is better to spare, put its free it, the method to merge its favorite class, maybe they live together more appropriate, you must You will find a suitable person.

Extraction change

If you join a new type of business (eg payment channels, database type, etc.), you need to change in order to achieve a lot of places, which means that there is room for improvement, can cause changes to do for reasons out of configuration and change methods placed in a class, so not only can do to modify a deal with change, also can clearly know the way will be affected.

Combination of tools

A language contains many of the basic types with built-in method, but can not meet all needs, such as the amount of unit conversion, Time Array format conversion, UUID generated and so simple and easy to overlook small features, high frequency if these features appear, the rules will change bring a series of changes, then you can consider these small features abstract methods as a tool, and a combination of these methods as a tool class.

Obscenity function

Some logical that in the future there will be some changes, then placed a lot of hook method to deal with non-essential special circumstances, which tends to increase the complexity of the system and understand the costs, if the placement of the hooks can be used and valuable, then use, otherwise it is better not to obstruct the line of sight on the code.

Reducing switch

If we were to do a support micro-channel, pay channels treasure, Merchants Bank and other payment platform, the need butt different channels, because different channels docking mode, you need to use the switch to the type select the corresponding channel butt joint, but many places are likely to be used this switch, once the new channels will join over the world where to find uses switch.

The switch statement can be transplanted into separate methods, these methods make up the base class, subclass corresponding to the case statement calling the specific implementation so that subclasses to complete, adding such payment channels and changes only need to modify a class.

By eliminating unnecessary class

Each class is created, there are other people in terms of understanding the costs, if ever, for a change in the added class, has not changed in the actual scene, then put the class to remove it, we need to really have value, understanding of low-cost systems.

A class will set some variables set as a special case, these variables will not necessarily be used, handling code you have to guess who aim was to set these variables, makes very large head, as these variables and related methods in a separate a class, details of the shield, the desired function is expressed by the method, it will make more efficient extensions.

Remove "ghost class"

Project occasionally there will be some "ghost classes", these classes do not do the actual work, but is responsible for calling other classes, as this "middleman" to remove, so that class and the caller actually be called a relationship.

Abstract base class of the same class

If two classes, a few methods in which the same effect, with different names, it can be two similar classes or by modifying the name of transplantation manner consistent, then the two classes abstract base class, so as to expand.

Add notes

Notes multi not a bad thing, it is a reconstruction leads the way, when you feel the need to comment on a piece of code to write, it means that you think this code is not easily understood by others, this is also the side that heavy issued a warning signal structure, so when you want to write notes, on the first reconstruction, fight for comment becomes superfluous.

Guess you like

Origin www.cnblogs.com/kyoner/p/11030126.html