当前位置:网站首页>Fear of reconstruction? I'm too late to tell you how to refactor. Now I'm here

Fear of reconstruction? I'm too late to tell you how to refactor. Now I'm here

2020-11-09 22:37:00 Java Architect Alliance

A few days ago, I happened to see the content of an online friend , It's said that the old system crashed after changing a line of code , It's really bald . The more successful the company is , The more old systems and legacy code that can't be counted , In particular, code related to basic services , It's a real-life version of a gourd that floats . Start up companies are better , There is no burden of history . We also often refactor old code , If for no other , It's just that I'm afraid it's moldy for too long . It happens that I'm also doing system refactoring recently , Sum up what we're doing and some tips .

Code can get moldy, too

It's not just food that molds , The code will also . We usually call it corruption . The process of corruption happens every day , An urgent need , A new colleague joins in , A perverted problem fix , A co construction project , wait . corrupt , It can never be avoided , Just like the entropy of the universe is irreversible . In the face of corruption , Architects often add a coating . If your system doesn't have an anti-corrosion coating , Think about it now . Even if there's an anti-corrosion coating , Also can not withstand the devastation of the years , It's like hair's thinning out , That's life .

therefore , We need to refactor . It takes a lot of work to reverse entropy , To sort out the messy code again , Make it clear again 、 A state of well-defined architecture .

carding , Or comb

Spend a lot of time sorting out the historical logic before reconstruction , And be careful . The ancients said “ A good workman does his work well , You must sharpen your tools first ”. The logic sorted out is the guiding light of reconstruction . What to discard , What needs to be optimized , What to refactor , Which products need to be repositioned , What are the risk points , After carding, it is basically clear that . It's not just the emergence of logic that brings about , There may also be the direction of Architecture . By combing , Be able to clearly discover business logic , You can even define new domain models 、 value 、 Events, etc. . Think of the Galactic era , Fight with the real-time star map of the galaxy , You know how important it is to comb .

The playback , Contrast changes

After refactoring , The test should cover all aspects of . Now , Playback is very important . Ali also open source a playback tool jvm-sandbox-repeater.GitHub Address https://github.com/alibaba/jvm-sandbox-repeater. Through playback , It can be in the pre launch environment debug Online problem , You can see the actual performance of online real traffic in the pre launch environment . by force of contrast , You can find out what's different after refactoring , In particular, page rendering and persistent data .

After the playback is finished , It can also be used as a quick verification of daily releases . As long as there is no difference between this playback and the last normal playback , Basically, the risk is very small .

framework , Start with the end

Now that we're doing refactoring , In terms of architecture, we should design well . Try to get rid of the intricate historical logic , Design a new architecture . To improve R & D efficiency 、 Reducing maintenance costs is the ultimate goal , All of the refactoring design revolves around this goal . Nothing can't be changed , If not , Then add two better programmers . If the refactoring keeps a pile of shitty legacy code , I don't know the meaning of refactoring . Refactoring is about starting with the end , In the new architecture design , Let legacy code be reincarnated to get new life .

improve , Code refactoring

Good programmers need to refactor existing code more or less from time to time . stay 《 restructure Improve the design of existing code 》 In a Book , The author has given many concrete methods of refactoring . Duplicate code extraction 、 Too long function split 、 Model redesign 、 Encapsulate fields 、 Encapsulation set 、 With State/Strategy Replace type code 、 Methods moving position and many other skills , It's not going to unfold here . I think every programmer should study this book well , And then go deep into code level refactoring . As the book review said “ You shouldn't go through the refactoring manual , But it's important to know what's mentioned in this book 70 There are several reconstruction methods in mind ”.

Speed , A quick victory

A lot of legacy code is already like online loan , Deeper and deeper . The return on the resources invested , It's actually much higher than maintaining old code . The process of refactoring should be fast , During the process, daily needs should be suspended as much as possible . Refactoring is a lot of work , But it's also very fast . If you reconstruct , One side of the line is still changing requirements , It's likely to get into trouble , Even lost online logic after refactoring . It's done , The playback is done , Programmers can quickly refactor as planned , A few more people , Make sure it's done quickly .

State of mind , Calibrated boldness

All the work ahead has been done , Then the next step is to do .

Don't worry about it , Don't particularly . This code does not understand how to do , Change ! Watch the replay . This code is stinky and hard , Change ! Watch the replay .

When we change it , Be careful, too , Understand the original business logic . Defy the enemy strategically , We should also attach importance to the enemy tactically . do code review, Let the people who know see the changes together , Or team members to check the gate together .

So , I'd like to recommend this book ---《 restructure 》, I'd like to share it with you

Adapt to people

The target audience of this book is Professional programmers , Those who write software for a living . Examples and discussions in the book , It involves a lot of code that needs to be read and understood in detail . These examples are all in the form of Java It's written in . The reason for choosing Java, Because it's a language that is more and more widely used , and Anything with C Language background People can easily understand it .Java Is an object-oriented language , The object-oriented mechanism is very helpful for refactoring .

Although the focus is on Code , but Refactoring for system design It also has a huge impact . It's also important for senior designers and architects to understand the principles of refactoring , And use refactoring technology in your own projects . It's better to have seniority 、 Experienced developers introduce refactoring techniques , Because such people are the best at understanding the principles behind refactoring , And adjust it according to the situation , Apply it to a specific area of work . If you don't use Java, This is especially important , Because you have to rewrite my example in other languages .

Now I want to tell you , How to make full use of it without reading the whole book .

If you want to know why you should refactor , Please read the first two chapters . They tell you what refactoring is and why it should be refactored .

The first 1 Chapter reconstruction , The first case

The first 2 Chapter reconstruction principle

If you want to know where to refactor , Please read before 5 Chapter . It will tell you some code features , These characteristics point to “ We need to refactor ”.

The first 3 Bad smell of chapter code

The first 4 Chapter 2 constructs the test system

The first 5 Chapter refactoring list

The first 6 Chapter reorganizing functions

The first 7 Chapter moves features between objects

The first 8 Chapter reorganizing the data

The first 9 In this chapter, we simplify the conditional expression

The first 10 Chapter simplifying function calls

The first 11 Chapter deals with general relations

The first 12 Chapter large reconstruction

You need this information , Focus on + After forwarding , Direct messages ” Information “ You can see how to get it

If you want to start refactoring , From 5 The chapter extends to 12 Chapter . It can't be said to be a comprehensive list , It's just a start , It includes all the refactoring techniques I've sorted out in my work so far . I hope this is a part that you should review again and again in the future . Just skim the list at first , Look at some of them , You don't have to understand all the details . Once there is a real need to implement a code , Read it in more detail , Get help from . The list section is for reference , You don't have to read it all at once .

In addition, you should also read the list of other authors “ Guest section ”, Especially in the first 15 Chapter .

The first 13 Chapter reconstruction , Reuse and reality

The first 14 Chapter refactoring tool

The first 15 Chapter summary

So-called ** restructure (refactoring)** It's a process like this : Without changing the external behavior of the code , Make changes to the code , To improve the internal structure of the program . Refactoring is a methodical method of program arrangement which has been formed through a lot of efforts , It can minimize the possibility of introducing errors in the finishing process . In essence , Refactoring is to improve the design of the code after it is written .

All right. , You need this information , Focus on + After forwarding , Click here to view the information access method

版权声明
本文为[Java Architect Alliance]所创,转载请带上原文链接,感谢