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
  1. Determine whether the meaning expressed is consistent.
  2. Whether references are made elsewhere
  3. 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
  1. Short function objects are easy to understand, easy to read, and beautiful to bring all the benefits:
  2. Ability to explain, ability to share, ability to select
  • Principles for designing short functions
  1. Whenever you feel the need to comment something out, write it in a separate function named after its purpose.
  2. 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
  1. 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

  1. Pay attention to conditional expressions and looping programs

Large Class = Large Class

  1. Notice that individual classes that do too much are probably too large
  2. There’s too much code

Long Parameter List

  1. 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
  2. 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.

  1. Design software that can be easily modified with minimal modifications.
  2. 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

  1. Meaning: Indicates that the modification of one place requires the modification of multiple places in the program.
  2. Put the code and functions that need to be modified into one class

Feature Envy

  1. Because of the data, a function may depend on many functions to function properly.
  2. Putting together things that are always changing. (The data and the behavior of referring to it always change together)
  3. Always keeping change in one place

1. It was Clumps of Data

  1. Look for classes that have a lot of fields and arguments. These are data pellets that need to be broken up.
  2. Delete one of the many items of data and create a new object for them.
  3. Reduce the number of fields and arguments, and make calls with new objects as appropriate.

– Primitive Obsession.

  1. Structural types allow you to organize data into meaningful forms, and basic types are the building blocks that make up structural types.
  2. The value of an object: It blurs the line between basic data and larger classes.

Switch Statements(Switch Thriller appears)

  1. Using polymorphism to solve the problem of switch language in process oriented (the problem of switch statement is repetition)

Parallel Inheritance Hierarchies

  1. Using an instance of an inheritance system to refer to another inheritance system instance

Message Chains

  1. There are many nested calls between functions. If a variable of a function changes, many functions need to change, affecting the whole body.
  2. 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

  1. 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.