Refactoring improves the design of existing code
In the daily coding process, these knowledge points may be very easy to ignore or due to the coding habits of the wrong place
Meaning of software engineering: hope to establish perfect requirements and design, according to the existing rules to write standard uniform code, this is the beauty of structure; Rapid iteration and RAD subvert the myth of “omni-knowing”, solving problems in a way that is almost a knife and axe, and achieving requirements in a cycle of chaos, which is the beauty of deconstructing.
Duplicated Code
- The two pieces of code in the program are extremely similar
- Determine whether the meaning expressed is consistent.
- Whether references are made elsewhere
- Merge it into a function in a class and simplify code reuse by calling the function.
- For repetitive code, the refiner needs to think carefully, and the refined function is more suitable in that location, to ensure that the function is unique.
Long Method (Long Method)
- Avoid functions (methods) in programs that are too long
- Short function objects are easy to understand, easy to read, and beautiful to bring all the benefits:
- Ability to explain, ability to share, ability to select
- Principles for designing short functions
- Whenever you feel the need to comment something out, write it in a separate function named after its purpose.
- The key is not the length of the function, but the semantic distance between what the function does and how it does it
- How to design short functions
- Look for comments
Comments can express the specific meaning of the function, showing the semantic distance between the purpose of the code and how it is implemented;
Even a comment in a function, if the comment is intended to be illustrative only, is necessary to refine its design into a single function
- Pay attention to conditional expressions and looping programs
Large Class = Large Class
- Notice that individual classes that do too much are probably too large
- There’s too much code
Long Parameter List
- Make good use of the concept of objects. Not everything a function needs needs to be passed as arguments, only what it currently needs or can obtain on its own
- Learn to use objects to pass parameters (parameters are hidden in objects for later maintenance and upgrade)
Note: If the parameter column is too long or changes too frequently, you need to reconsider your dependencies.
They all have Divergent changes.
- Design software that can be easily modified with minimal modifications.
- All modifications to external changes should only occur in one class, and everything in the new class should reflect this change.
Meaning: A class is subject to multiple changes
Shotgun Surgery
- Meaning: Indicates that the modification of one place requires the modification of multiple places in the program.
- Put the code and functions that need to be modified into one class
Feature Envy
- Because of the data, a function may depend on many functions to function properly.
- Putting together things that are always changing. (The data and the behavior of referring to it always change together)
- Always keeping change in one place
1. It was Clumps of Data
- Look for classes that have a lot of fields and arguments. These are data pellets that need to be broken up.
- Delete one of the many items of data and create a new object for them.
- Reduce the number of fields and arguments, and make calls with new objects as appropriate.
– Primitive Obsession.
- Structural types allow you to organize data into meaningful forms, and basic types are the building blocks that make up structural types.
- The value of an object: It blurs the line between basic data and larger classes.
Switch Statements(Switch Thriller appears)
- Using polymorphism to solve the problem of switch language in process oriented (the problem of switch statement is repetition)
Parallel Inheritance Hierarchies
- Using an instance of an inheritance system to refer to another inheritance system instance
Message Chains
- There are many nested calls between functions. If a variable of a function changes, many functions need to change, affecting the whole body.
- Refactor any object on the message chain: First see what the message chain ends up with and see if you can combine multiple messages into a single function for processing.
The Middle Man
- Encapsulation: hides the details of its interior from the outside world.
Small remarks
Life is short, I don’t want to pursue what I can’t see, I just want to grasp what I can see.
I am zai said, thank you for reading, if it is helpful to you, trouble point like, forwarding thank you.