Refactoring overview-six reconstruction modules

Source: Internet
Author: User

Refactoring Method Introduction:

Refactoring improves the design of existing code

Reorganizing a function
  • Comments: Use as few comments as possible. The more comments, the worse the readability of the Code. The comments can be replaced by the function name instead of the length of the function name, it doesn't matter even if the function name is longer than the code in the function;
  • Extract Functions: Sort functions so that the function can encapsulate code properly. The function can effectively replace comments. A function that is too long will contain a lot of information, and the code will become poorly readable, and there will be a lot of comments. A line of comments will be added every other segment, which will make the readability worse. For example, we have commented out the relevant code in a method. Here we can extract the relevant code and encapsulate it into a function, use the function name to express the action to be executed in this Code. This method is called "extraction function ".
  • Inline functions: The opposite method of refining a function is "inline function", which replaces the code in the function with the position where the function is called. In the process of refining a function, the granularity of the function is too small, and the function does not do anything substantive, you can restore the function to the code.
  • Process local variables: The biggest difficulty in refining functions is processing local variables. Some simple local variables can be solved by "replacing temporary variables with queries". For variables that reuse multiple assignments multiple times, if this variable is not required by a loop, it is not a variable used for statistics. The two values of a variable are not associated. You can set two variables to replace the reused variables. For more complex local variables, methods can be extracted into a class, which is the ultimate trick to extract functions.
  • Remove the parameter value.: Do not operate on parameters in functions. If you need to operate on parameters, you can set a value for receiving parameters for a temporary variable.
  • Replacement Algorithm: After the function is reorganized, if you find that the algorithm can be improved to more clearly indicate the meaning of the function, you can replace this algorithm;
2. moving between objects
  • Category of the function module: In object design, it is one of the most important tasks to place a function module in a class. No one can guarantee that the design is completely appropriate at the beginning, this requires the reconstruction method "moving between objects.
  • Moving functions and fields: These two reconstruction methods can solve most of the problems. If the two methods are used at the same time, first move the field and then move the function.
  • Refining and linking classes: If a class has too many responsibilities, it will become bloated. In this case, some functional modules can be separated to extract another class. If a class has too few functional modules, this class is very small. You can join the class and merge the class into other function-like classes.
  • Hide delegate relationships and remove middlemen: When a user calls another class through one class and needs to use the functions of another class, in order to achieve better encapsulation, the delegate relationship can be hidden and a delegate function can be added to the server, allows users to directly access this class to avoid users' understanding of the delegation relationship. If users frequently access other classes through this class, a delegate function is added for each access, if this process is too complex, we can remove the man-in-the-middle and cancel the delegated relationship, which is opposite to hiding the delegated relationship.
  • Introduce additional functions and introduce local extensions: When we want to add a function to a class, we cannot operate on the class, but can introduce an additional function. When many features are added, you can put the functions you want to add in a class. We introduce this class as a whole, that is, introduce local extensions.
3. reorganizing data

  • Self-encapsulated Field: Add the get and set methods to the private member variables in the class. Note that the added get method has no modifier and must be separated from the following encapsulated fields. there is always a debate about the methods used for data in the category. you can use the direct access method to access the function. If a problem occurs, use the field of the function in the functions class.
  • Replace data value with object: A simple numeric value is often used to express a certain concept in programming. We can create an object to express this change and convert the dumb data into objects that are good at expression, it can make the program more beautiful.
  • Replace arrays with objects: If you find that the behavior of an array is similar to a data structure, you can change the array to an object and add the corresponding text method of the array to make the data structure clearer.
  • Magic count: A number with a special meaning. Avoid the existence of a magic number. Use a constant instead of a magic number.
  • Unidirectional Association and bidirectional Association: The association between classes is to define an object of another class as a member variable in one class. One-way Association is to have a member variable of another class in one class, the other class does not have a member variable of this class; the two-way association means that the object of another class exists in both classes as the member variable. in the case of one-way association, if we need a reverse pointer, such as a customer and an order, we need to know how many orders a customer has and which customer the order belongs, in this way, we need to convert one-way Association to two-way Association.
    . A large number of Bidirectional associations may lead to many zombie objects. An object is useless, but other Classes define the modified object as a member variable, in this way, we need to change this two-way Association to one-way Association;
  • Copy monitored data: Sometimes the user interface processes the business logic. To move the business logic to the appropriate domain class, you need to save the data related to the business logic in the domain class, repeated data here is inevitable. The priority of the layer is higher than that of the data duplication.
  • Encapsulate fields, encapsulate sets, and replace records with data classes: The encapsulated field is similar to the self-encapsulated field, but it is different. Add the get and set methods to a public member variable. The modifier of the get and set methods is public. encapsulate a set. To encapsulate a set, try to avoid exposing the entire set to the user. It is best to expose the query method, add, and delete the elements in the set to the user. replace records with data classes. There are many record structures in the programming environment. To operate these record structures, you need to create an interface class.
    To process the external data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.