Talking about the problem of code refactoring and optimization

Questions about code refactoring and optimization are often encountered when doing front-end development. During this period of time, it may be that the self-requirement is relatively high, not only the project can be completed, but the functions can be used normally. I also tried my best to study how to write elegant code, better performance, and more maintainable code. The popular point is refactoring. This article is a small record of mine. It is also simple to share examples here. In-depth and complex examples will be written and shared in the future when there are suitable examples.

 

First you need to understand what is refactoring? Actually, refactoring is not rewriting. Refactoring roughly means using a series of refactoring methods to change the internal structure of the project without affecting the functional use of the project. Improve readability and maintainability within the project. No matter what the project is, there is an iterative process from simple to complex. In this process, without affecting the use of the project, it is necessary to continuously optimize the code to maintain or increase the readability and maintainability of the code. In this way, you can avoid the need for a lot of communication and communication in team collaborative development. to join the development of the project.

 

Why do you need to refactor? Just like washing clothes when they are dirty, mending when they are broken, and throwing them away when they don’t fit. Shangxuetang Baizhan Programmer Mr. Chen pointed out that with the continuous increase of business requirements, changes and abandonment, the code of the project will inevitably have defects, which will affect the readability, maintainability of the code, and even affect the performance of the project . The purpose of refactoring is to solve these defects and ensure code quality and performance. But the premise is that it cannot affect the use of the project.

 

As for the reasons for the refactoring, I have summed up the following points:

The function logic structure is chaotic, or because there are no comments, it is difficult for even the original code writer to sort out the logic.

Functions have no scalability at all, and they cannot be handled flexibly when new changes are encountered.

Due to the strong coupling of objects or business logic, the code of business logic is huge, and it is difficult to troubleshoot during maintenance.

Too much repetitive code, no reusability.

As technology evolves, code may also need to be modified with new features.

With the deepening of learning, is there a better solution for the previous code.

Because of the way the code is written, although the function is used normally, it consumes a lot of performance and needs to be optimized by changing the solution.

 

So when do you need to refactor? At the right time, at the right thing. In my understanding, refactoring can be said to be the development and maintenance cycle that runs through the entire project, and can be regarded as a part of development. In layman's terms, at any time during development, as long as you see that the code is awkward and stimulates obsessive-compulsive disorder, you can consider refactoring. However, please refer to the following points before refactoring.

First, refactoring is something that takes time to do. It may take more time than the previous development time.

Second, refactoring is to optimize the code, provided that it does not affect the use of the project.

Finally, the difficulty of refactoring varies, it may only be slightly changed, and the difficulty may be more difficult than the previous development.

Based on the above points, you need to evaluate whether to refactor. The evaluation indicators can refer to the following points

Quantity: Whether too much code needs to be refactored.

Quality: Readability, maintainability, code logic complexity, and other issues, whether the impact on the quality of the code has reached an unbearable level.

Time: Whether there is ample time for refactoring and testing.

Effect: If the code is refactored, what are the improvements, such as improved code quality, improved performance, better support for subsequent functions, etc.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325050875&siteId=291194637