cover

Let's talk about "refactoring".

sorcererxwβ€’

Recently, I finished reading "Refactoring: Improving the Design of Existing Code" (https://book.douban.com/subject/4262627/). The book introduces many techniques for refactoring, but because I have previously studied "Effective Java" and design patterns, and I am quite particular about code cleanliness in my daily development, I have spent a lot of time thinking about code design. Therefore, I didn't find some of the techniques mentioned in the book particularly eye-opening. It was just the first time I saw someone systematically documenting these techniques.

What has had a greater impact on me than the specific techniques of refactoring is the author's philosophy on refactoring.

Refactoring is like exercising and eating the right food.

Refactoring is a major undertaking, but it should be integrated into everyday development.

Just like humans, regular exercise and a balanced diet may take some time in our daily lives. However, adopting a healthy lifestyle can greatly improve our efficiency in both work and daily activities. In the long run, it will contribute to a healthier physical condition. On the contrary, neglecting our health can not only decrease our productivity but also lead to potential health risks.

If we focus on timely code refactoring and ensure proper code design in our daily work, not only will it improve our development efficiency, but it will also ensure the robustness of the future system. On the contrary, neglecting code maintenance not only reduces development efficiency but also accumulates technical debt.

Two Hats

The development process can be divided into two aspects: feature development and code refactoring. The former is aimed at the computer, while the latter is aimed at developers.

It is important to remember that these two tasks cannot be carried out simultaneously, just like wearing two hats at the same time:

  • If you are working on developing a specific feature, do not refactor a portion of the code halfway through. During this process, your goal is to implement the functionality, not optimize code design.
  • If you are refactoring, don't rush to fix bugs when you encounter them. Take note of them and come back to them later. Focus on code refactoring, because the essence of refactoring is to optimize code design while ensuring that the code functionality remains unchanged. Even if you fix a bug, it can still result in changes to the final functionality (input/output).

I deeply resonate with this point because during regular development, I often find opportunities to optimize certain code designs or realize the need for refactoring to update functionality. However, if I mix these two tasks together, it can result in incompatible code and ultimately turn the code into a tangled mess.

I think it's mainly because when developing a feature, our focus is narrowed down to a specific area, while during refactoring, we need to take a broader view of the entire module's design. It's impossible for a person to simultaneously concentrate on both aspects.