First step: Start with the decomposition of large functions
1. What is a large function?
The big function is the super method that the business logic is especially complicated, the program code is very many, and the one that brings up a headache. Super-large functions are hard to read, more difficult to maintain and change, and are undoubtedly the hardest hit in software degradation.
2. How do I solve super-large function problems?
The most effective way is decomposition, according to the function step by step decomposition, restore its due optimization structure. This process is often called "extraction methods". Refactoring is a process of exploration.
3. Extraction Method:
A. When the code is atomized, give the function an understandable name. At first our understanding of this code may not be so deep, so we often choose to use the result variable to name it, as we have a deep understanding of the code, you can use the "rename method" to re-name it according to the code intent.
B. The extracted code must be functionally cohesive, meaning that they perform a Chiang Dao, clearly defined function. At the same time, it must only perform a clear function.
C. Improving code reuse becomes an important part of code optimization, so repeating code is also an important indicator of extracting functions.
D. Some block operation statements, such as conditional statements, loop statements, try statements, can be flags of the extraction function. The most typical is the IF statement, which is often a relatively independent function contained in an IF statement.
4. Frequently Asked Questions
A. The biggest difficulty is how to deal with the data exchange between the extraction function and the original function, that is, the setting of the function parameters.
B. Handling when multiple return values are required
C. Naming problems with extraction methods
Step two: Split large objects
1. In the face of the super-large function, we take the idea of splitting it into a function as the core and splitting it into a separate function.
2. Take responsibility-driven design thinking as the core, adjust our program structure, build high-cohesion, low-coupling software system.
3. What is a responsibility-driven design?
Responsibility-driven design is the requirement that all classes and interfaces we design have their own responsibility definitions. All of the methods and properties within each class and interface are related to that responsibility and are highly relevant.
4. One of the most important principles in determining whether or not a function is relevant is whether they are the same cause of the software change.
5. The process of splitting large objects
Move some of the methods in the original object to another object, which is called the Extract class. The principle of mobility is responsibility-driven design.
6. How can I tell if a class has any behavior unrelated to his or her responsibilities?
A. Analytical domain model: A model analysis that should be completed in the late stage of demand analysis and design development. From the user's point of view, analyze the business domain related things, the attributes of things, the behavior of things and the relationship between things. Our analysis of domain models is a guarantee that our software systems are designed to correspond to the real world.
B. Analysis of reasons for business change: one responsibility is one reason for software change, which is also the essence of the principle of software design SRP, the single responsibility principle. At design time, the above business should be drawn out and assigned to the class that should have that responsibility.
C. Finding an information expert
7. What is an information expert?
An information expert is a business object that has some useful data in a software system.
8. When business requirements are the same in many places, what should be done when there is a lot of duplication of code?
A. The first step is to divide the same business and different business, the implementation of the program is the same code and different code. Keep the different code in the original program, the same code is extracted into a new function.
B. The second step is splitting, splitting the same business class into an abstract class of inheritance between normal classes. This process is called "extracting the parent class."
9. The principle of large object splitting is responsibility-oriented analysis and design, but this is not always the case in practical work.
To reconstruct the reading notes--a practical article