Chapter 2: Reconstruction
1. Refactoring is a process of modifying existing work code under conditions that do not change external behavior. In other words, it is to make changes to what to do. The purpose of reconstruction is to improve internal structures.
2. Reconstruction is related to TDD in two aspects. Most of its work is to eliminate repetitive code introduced to make testing; and that, TDD provides testing, let us let us go STAD.
3. If the job completed by the duplication is useful or important, we have to take the code out and then call it at each location.
4. If repeated appears in the inheritance level, then we may be able to push the duplicate code to a higher level.
5. If you repeat some of the structures of some code rather than specific details, then different parts are extracted, and the common structure part is made into a template method.
6. When encoding, add new features to the system but do not perform reconstruction; refactoring, changing the code but does not increase new features.
7. When the intent is not clear, it is generally necessary to refactor, a simple and powerful clarification method is to select a better name for transactions.
8. If you see a day comment or feel that it is necessary to write a comment, then you should first consider the reconstruction or rewrite code. Note indicates that those functional units should be extracted into an independent approach.
9. If there are multiple associated operations of operational data, consider the method of considering some operational data to merge into a class.
10. Avoid making a class know too much about the internal details of another class. To handle this situation, we need to migrate the method, so that the code that knows each other is in the same location. For example, don't read more information, but let that class itself to do yourself.
11. Inheritance will also allow the subclass to know the details of the parent class, need to privatize the ancestors (go, decouple), or change the inheritance to delegation.
12. Avoid a class of sizes too much, because there is too much work to be completed, there is too much thing that may be understood, maybe the condition is too much? If so, may extract a child class and use a polymorphism to see if the code can be properly executed. If there is a clear set of sub-function, then you may be able to put them out to form your own class. The size is too large, with the following characteristics: Run the code row (LOC) metric; view its UML map.
13. Avoid lazy classes, this class is just the opposite of the situation in the large size, and does not reflect the value of the class.
14. Avoiding the method is too long, you need to split the code in the respective independent methods.
15. Use the Switch statement uncharged (CASE statement) means that there is no in-depth understanding of the principle of object-oriented. It is generally possible to use a polymorphic, a better, more neat method to achieve the same purpose.
16. Avoid Shotgun Surgery, when we change your code, find that you need to change multiple places. Generally, the code can be refactored by using the method of "use polymorphism instead of conditional judgment".
17. Prepare automated tests before reconstruction to avoid reimbursement of the external behavior (Behavior) of the changed code;
18. Sometimes, using graphical perspective, observe things should be more likely to find problems.
19. Reconstruction method "Extract class": Extract a new class of multi-segment cohesive behavior. If you need a variety of implementations of a certain behavior, the interface can be extracted from the class.
20. Reconstruction Method "Extract interface": Perhaps the specific implementation is abstracted in order to more easily use a technology called analog object (Mock Objects), define each system through the interface Group important behavior is often very convenient.
21. Reconstruction Method "EXTRACT METHOD": When a method is too long or logic is too complicated, we can extract some of them to form their independent methods.
22. Reconstructing Method "Using subclasses instead of type code": If you want to use the type code to indicate the child type, you should use this reconfiguration method for each type of design subclass ( Solve the problem of Switch chaos).
23. Reconstruction Method "Replace Conditional With Polymorphism": The purpose and solution of the previous one is similar.
24. Reconstruction Method "Form Template Method": This reconstruction method can be used if there is a similar method having the same structure but different details in multiple classes. In the end, it is necessary to place this method of the same structure in the superclass and put the extracted specific method in the subclass, and the polymorphism will be responsible for calling that function.
25. Reconstruction Method "Introducted EXPLAINININININED": When the expression is complex and difficult to understand, we can extract some of these parts, keep the intermediate results in a clear temporary variable.
26. Reconstruction Method "Replace Constructor With Factory Method": For coding, the constructor can be privatated, and then the example is used to generate an example.
27. Reconstructing method: "Use the entrustment replacement": If you just use some of the class features of the superclass, you should use the delegate to use the "Class" as an instance variable to save the object.
28. Reconstructing Method: "Use symbol constants to replace the magic number": "This is more general, suitable for any value within the string.
29. The design mode is not simply spliced (Composites), and the design pattern is required to be an indexed target; the design model should not be changed without changing the design mode, but through the reconstruction gradually introducing design mode, and only when necessary .
30. In addition to the development code, the test code should be reconstructed.