"Refactoring _ Improving the design of existing code" (bottom)

Source: Internet
Author: User

Tenth chapter simplified function invocation
1. Rename Method function Renaming: The name of the function does not reveal the purpose of the function. Modify the function name.
A way to name a function: First consider what kind of comment to write to the function, and then convert the comment to the function name.
2, add Parameter parameters: A function needs to get more information from the caller. Add an object parameter to the function to bring the object into the required information for the function.
First to determine whether the existing parameters can meet the requirements, if not satisfied, why? The parameter queue to prevent too long.
3. Remove Parameter removes parameters: the function body no longer requires a parameter. This parameter is removed.
4. Separate query from Modifier separates the querying function from the modified function: A function returns both the object state value and the object state.
Create two different functions, one for the query and the other for the modification.
5. Parameterize method makes the function carry parameters: Some functions do similar work, but they contain different values in the function body. Create a single function that expresses those different values with parameters.
6. Replace Parameter with Explicit Methods replace the parameter with a definite function: you have a function that takes a different behavior depending on the value of the parameter entirely.
For each possible value of this parameter, create a separate function.
7. Preserve Whole object remains intact: You take a number of values from an object and use them as arguments for a function call. The entire object is passed instead.
8. Replace Parameter with Methods takes a function instead of a parameter: the object invokes a function and passes the resulting result as an argument to another function.
The function that accepts the parameter can also invoke the previous function itself. Let the parameter recipient remove the key parameter and call the previous function directly.
9. Introduce Parameter object introduces parameter objects: Some parameters always appear naturally at the same time. Replace these parameters with an object.
10. Remove Setting method removes the Set Value function: A field in a class should be set when the object is created, and then it will not change. Remove all the set-valued functions for the field.
11. Hide method Hidden function: There is a function that has never been used by any other class. Modify this function to private.
12. Replace Constructor with Factory Method replaces the constructor with the factory function: you want to create the object not only to do simple construction actions.
Replace the constructor with the factory function.
13, encapsulate downcast package down transformation: The object returned by a function needs to be performed by the function caller to perform a downward transformation. Moves the down transition action to the function.
14. Replace error code with Exception to replace the fault code with an exception: a function returns a specific code that represents some kind of error condition. Use exceptions instead.
15. Replace Exception with test to replace the exception: you throw an exception in the face of a condition that a caller can pre-check.
Modify the caller so that it checks before calling the function.

The 11th chapter deals with the generalization relationship
1. Move Up Field field: Two subclasses have the same field. The field is migrated to the superclass. The so-called same refers to the use of similar methods.
2. Pull up Method function moves up: Some functions produce the exact same effect in each sub-class. The function is migrated to the superclass.
3. Pull up Constructor body constructor body Move up: You have some constructors in each subclass, and their ontologies are almost identical.
Create a new constructor in the superclass and call it in the subclass constructor.
4. The Push down Method function moves down: a function in a superclass is only related to some, but not all, subclasses of a class. Move this function to the relevant subclasses.
5. Push down Field field Move Down: A field in a superclass is only used by some, but not all, subclasses. Move this field to the subclasses that need it.
6, Extract subclass Refining Subclasses: Some of the attributes in a class are only used by some, but not all, instances. Create a new subclass to move the part of the attribute described above to the subclass.
7, Extract superclass Refining Super class: Two classes have similar characteristics. Create a superclass for these two classes and move the same attribute to the superclass.
8. Extract Interface Refining Interface: Some customers use the same subset in the class interface, or the interfaces of two classes are partially the same. The same subset is refined into a separate interface.
9, Collapse hierarchy Folding inheritance system: There is no big difference between the superclass and the sub-class. What they are.
10, Form template method to shape the templates function: You have some subclasses, where some of the corresponding functions perform similar operations in the same order, but the details of each operation are different.
Put these operations into separate functions, and keep them with the same signature, so the original function becomes the same, and then moves the original function up to the superclass.
11. Replace inheritance with delegation replaces inheritance with a delegate: A subclass uses only partial interfaces of the superclass, or data that does not need to be inherited at all.
Create a new field in the subclass to hold the superclass, adjust the sub-class function, make it delegate the superclass, and then remove the inheritance relationship between the two.
12. Replace delegation with inheritance replaces the delegate with inheritance: You use a delegate relationship between two classes and often write many very simple delegate functions for the entire interface.
Let the delegate class inherit the Trustee class.

The 12th chapter of large-scale reconstruction
1, for large-scale reconstruction, it is necessary to build consensus for the team members, the entire team must be aware that we are restructuring, everyone should arrange their own actions accordingly.
2, tease Apart inheritance comb and decompose the inheritance system: a succession system at the same time assume two responsibilities. Build two inheritance systems and let one of them call another by delegating relationships.
3. Conver procedural design to Objects the design of the process into objects: You have some code of traditional procedural style in hand.
Turns the data record into an object, divides a chunk of rows into small chunks, and moves the behavior into the related object.
4. Separate domain from Presentation separates the domain from the presentation/display: Some GUI classes contain domain logic. Separate domain logic and create separate domain classes for them.
5, Extract Hierarchy refining the Inheritance system: you have a class that does too much work, and part of it is done with a lot of conditional expressions.
Establish an inheritance system that represents a special case with a subclass.

The 13th Chapter reconstruction, Reuse and reality
1, reconstruction requires awareness, determination, technology, may also need a good team.
2, how to let the leadership, user recognition of reconstruction? Need to describe the benefits of refactoring in the short term.
3, reduce the cost of refactoring: for example, into the daily development. Time will prove the benefits of refactoring.
4. Re-construct safely:
1) Take advantage of the code-checking verification capabilities provided by these tools, such as compilers, test suites, code reviews, and more.
2) a dedicated refactoring tool.
3) The large refactoring is decomposed into small refactorings, and the security of each small refactoring is ensured.
5, from refactoring to software reuse and technology diffusion. Some principles:
1) software reuse and technology diffusion require software developers to have some new ideas, and they must be very concerned about these technologies.
2) The acceptance of the technology can be described as a bell-shaped curve: The first segment includes the forerunner and the early adopters, the medium term includes a sharp increase in the population (early consumers and late consumers), the latter part is
Those who are slow to act. Pioneers focus on new technologies, early/late consumer main relationship maturity, cost, support and so on. If you want your technology to be accepted by everyone,
It is important to solve these differences. Because different people are concerned about different aspects.

14th Chapter Refactoring Tools
1, there is no automated refactoring tool is one of the reasons most people do not want to refactor. Manual refactoring costs too much.
2, the automatic reconstruction tool should have the technical standard:
1) Use semantic analysis to set up a program database for each word.
2) parse tree, which is used to represent the function structure.
3) accuracy.
3, the automated refactoring tool should have a practical standard:
1) Speed
2) Revocation
3) integration with other tools

The 15th chapter summarizes
1, how to judge their own reconstruction is "enlightenment"? When you can stop refactoring with confidence. It may be that the target of the refactoring is all implemented, or it may be just a part of it, and more likely everything is back to the starting point.
In this process, you will learn a lot. Refactoring has no end point, no deadlines, and perseverance is what is needed.
2. How to learn refactoring:
1) Pick a target
2) Stop when you're not sure
3) Learn the original way back
4) Duet

"Refactoring _ Improving the design of existing code" (bottom)

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.