Life goes on, reconstruction goes on

Refactor

Insert picture description here
The so-called refactoring is the process of
making changes to the code without changing the external behavior of the code to improve the internal structure of the program. Make the design mode and structure of the program more reasonable, and improve the scalability and maintainability of the software.

At work, I chatted with other colleagues about the projects that I had jointly developed a few years ago, talked about some of the problems found in the original framework, and discussed better solutions.
Under his recommendation, I entered the book " Refactoring and Improving the Design of Existing Code, 2nd Edition".
A good memory is not as good as a bad pen.
I am here to record the notes of reading this book.

Their opinion

The following are excerpts from people in the industry reading this book:

  • For software engineers, refactoring is not extra work, it is coding itself. ——Qiao Liang, Tencent Senior Management Consultant

  • The pursuit should be to write code that can be read by humans, not only by machines. ——Hua Yan, Architect of JD 7FRESH

  • The software never dies, and the reconstruction never stops. ——Yu Sheng, translator of "The Way of Code Conciseness: The Professional Qualities of Programmers"

  • Continuously optimizing the existing code is the best way to maintain the vitality of the system. ——Yang Weihua, Deputy General Manager of Weibo R&D

  • Deeply understand the architecture, understand the business, understand the requirements, and reduce the useless repeated refactoring due to design errors. ——Zhuang Biaowei, Director of Open Source Community

Restructuring mind

Wang Jian's (translator and colleague) 16-character reconstruction mentality: the
old remains unchanged, the
new is created,
one step is switched, the
old goodbye.

Refactor the first example

Split a complex single method into multiple methods.
The main method function is readable and easy to understand.
There is a tendency to organize paragraphs for a very long article and refine the title; it makes it easier than people to understand the general content of the article.

The first example uses the following reconstruction methods:

  • Refine function (extract a piece of code separately into a function)
  • Query replaces temporary variables (function parameters: cancel variables that can be obtained from existing parameters)
  • Inline variables (inside the function: cancel temporary variables)
  • Change the function declaration (modify the function name so that it can clearly describe its role)
  • Move statement (inside the function: move the variable declaration to the position next to the loop)
  • Split loop (create multiple logic under the same function separately to create a loop)

About program performance

Refactor first, then perform performance optimization.
In the process, some program performance is sacrificed, but the impact on the reference is minimal.
Don't panic when it has a major impact on performance. The first reconstruction is for better tuning later.

Good habit of refactoring

  • Before refactoring, prepare a set of reliable tests. These tests must have the ability to self-check.
  • Modify the program in small steps so that problems can be discovered when errors occur, and you can't rush for success. This is especially important for systems that are already online.
  • Before pushing the code to the remote repository, compress the fragmentary changes into a more meaningful commit.

Coding style

Variable rename

  • Always name the return value of the function "result".

Amount calculation

  • Storing currency in units of cents can avoid the use of floating point numbers without affecting mathematical operations.

Guess you like

Origin blog.csdn.net/u011513460/article/details/105674265