"The Timing and Method of Refactoring", a book worthy of careful study by programmers

       The existing code structure and framework have been used for a long time, and new functions have been continuously added to it. The maintainability and scalability have become worse and worse. As the demand continues to increase, the existing code has become more and more bloated and complicated. It becomes difficult to maintain, and even serious performance bottlenecks appear. Generally, at this time, we may need to improve and refactor the code. Through refactoring, the coupling of multiple modules can be reduced, the scalability and maintainability of the code can be improved, and the performance of the program can be effectively improved. Today we will talk about refactoring related content.

1. What is refactoring?

       Everyone has their own definition of everything. In order to achieve the best effect in communication, the definition of refactoring is given here. Depending on the context, the word "refactoring" has two different definitions, the first in the noun form and the second in the verb form.

  • Refactoring (noun): An adjustment to the internal structure of software with the aim of improving its comprehensibility and reducing its modification cost without changing its observable behavior.
  • Refactoring (verb): Use a series of refactoring techniques to adjust the structure of software without changing its observable behavior.

       Is refactoring just tidying up code? In a way, yes. But I think refactoring goes beyond that, as it provides a more efficient and controlled technique for tidying up code. The purpose of refactoring is to make software easier to understand and modify. You can make many changes inside the software, but you must cause little or no change to the software's observable external behavior. Contrast that with performance optimization. Performance optimizations do not change the behavior of the component (except for execution speed), only its internal structure. But the starting point of the two is different. Performance optimization may reduce the understandability of the code, while refactoring is to improve the understandability of the code.

2. Why refactor

       Maybe your attitude is toward doing as little modification as possible: it works just fine anyway. You've got that old engineering adage stuck in your head: "If it ain't broken, don't touch it." The program may not be broken, but it hurts and makes your life harder, as the code is difficult to find Some BUGs are very difficult, and the unreasonable design leads to many changes to fix the BUG and increases the bloat of the program. In today's efficiency-driven development, the program will only gradually become complicated and difficult to control until no one is willing to take over or Working painfully.

        Refactoring helps you stay in good control of your code. Refactoring improves software design, refactoring makes software easier to understand, refactoring helps you find bugs, and refactoring increases programming speed. It sounds counterintuitive that refactoring can improve programming speed. I've always believed that good design is fundamental to rapid development -- in fact, rapid development is possible only with good design. If there is no good design, your progress may be fast for a certain period of time, but a bad design will quickly slow down your speed, and you will spend time on debugging and finding bugs. In the process of modifying BUG, ​​if you still use temporary modification where there is a problem, the number of BUG you modify in a short period of time will be amazing, enough to make the leader satisfied with your work, but this is a vicious circle.

3. When to Refactor

       People often discuss what refactoring is and how to arrange the refactoring schedule. Should two weeks be specially arranged for refactoring every month? I'm against setting aside time for refactoring in almost any situation. In my opinion, refactoring is not something that should be specially set aside for time. Refactoring should be done anytime, anywhere. You shouldn't refactor for refactoring's sake, like with design patterns, you can't pattern for a pattern's sake, you refactor because you want to do something else, and refactoring might help you do that well. So when exactly do you refactor? Here's when it should be refactored.

       According to the rule of three times, there are only three things, and three things are refactored. When you do something for the first time, just do it. If you do something similar for the second time, you will feel disgusted, but you can still do it anyway. If you do something similar for the third time, you should refactor. 

3.1. Refactoring when adding features

       At this time, the direct reason for refactoring is often to help me understand the code that needs to be modified-this code may be written by someone else, refactor the code so that I can understand it faster, and then I will refactor. The reason for this is to make it easy to understand the next time I look at this code, and a more important reason is that if the code structure is cleared up as I go forward, I can understand more from it. The code is often not designed to help me easily add the features I need. Whenever you look at a design that could be improved, say to yourself, "If you designed it a certain way, it would be much easier to add features." In this case, you can use refactoring to make up for the shortcomings of the early refactoring.

3.2. Refactor when fixing bugs

       Refactoring is used during debugging, mostly to make the code more readable. Whenever you get a bug report, it's a signal that refactoring is needed, because the code isn't clear enough—not clear enough for you to see the bug at a glance. At this time, more targeted refactoring can help deepen your understanding.

3.3. Refactoring when reviewing code

        The purpose of code review activities is to improve the development situation and spread knowledge among the development team. At this time, experienced colleagues can make good suggestions. You don’t have to imagine what the code should look like, you can “see” what it is, and you can get a higher level awareness, when refactoring not only has suggestions available, but many of them can be implemented immediately.

4. How to refactor

When you want to refactor the found code, you should use the following three steps to perform the refactoring.

  • The first step is to establish a reliable test environment for the code to be modified. 
  • The second step is to effectively grasp the timing of code refactoring and use scientific code refactoring methods! (For details, please refer to the classic book on refactoring recommended below)
  • The third step is to test the refactored code.

5. Recommend "The Timing and Method of Refactoring"

       "When and How to Refactor" is a book about refactoring techniques in software development. With its unique style and content advantages, it provides readers with comprehensive and easy-to-understand guidance to help them apply refactoring techniques in actual projects and improve code quality and development efficiency.

       This book has several important features:

  • Comprehensive System: This book covers all aspects of the refactoring field, from the basics to advanced techniques. Whether you are a beginner or an experienced developer, you will be able to gain valuable knowledge and guidance.
  • Easy to understand: The author pays attention to clarity, uses concise and easy-to-measure language, and avoids multi-professional terms and complicated concepts, which makes it easy for readers to understand and apply the refactoring techniques described in the book
  • Strong practicability: This book provides a large number of practical cases and source code examples to help readers apply the knowledge they have learned to actual projects. Through specific cases demonstrating different types of refactoring techniques, readers can better understand how to improve and optimize code.

        The book has received wide acclaim in the industry, and won unanimous praise from industry experts:

6. Finally

       "When and How to Refactor" is an important reference book to help software developers improve code quality and efficiency. Whether you are a beginner or an experienced developer, you can gain valuable knowledge and skills from it. By learning the refactoring techniques described in this book, you'll be able to write high-quality code that's easier to understand, maintain, and extend. Let's explore the world of refactoring together to create better software! Purchase link : click to view

Guess you like

Origin blog.csdn.net/chenlycly/article/details/131996656