How to refactor code-ideas and methods

Source: Internet
Author: User

First, I would like to talk about the experiences of several old projects. For old projects, most of them are in conflict or even a bit of contempt during their initial contact. I always like the previousCodePick out a bunchThen, I began to complain about code and previous developers. After a period of depressing complaints, I wanted to change my sense of responsibility, I hope to bring in the methods I think are good.The next step is to refactor the code.

Most developers may have experienced this experience, which is bitter.(Although refactoring is important, it is not widely recognized. At present, the domestic focus is on availability, and the quality of code has not been properly recognized.Important) Is also sweet (There will always be a rainbow after the storm). For young developers, the process of seeing a rainbow is painful and long. They all grow up in failure. In addition to experience, these failures are mainly due to the sharp effort and blind reconstruction!

Blindness is mainly reflected in:

1. If you do not have a clear understanding of the overall system architecture, you want to replace it with a new technology or architecture.

2,Do not analyze the existing system architecture orProgramThere are drawbacks, but simply talking about the design pattern and restructuring it with the one inherent in the design pattern (In refactoring, it serves only as a reference, not a referenceData.)

3,Refactoring is more convenient. Every version of development jumps out of the architecture and brings in new design ideas at will.

 

This blind refactoring brings more problems to the system:

You will find that your system becomes less efficient after reconstruction,

There are multiple ideas in the system at the same time, making it harder for new personnel to take over,

Because you do not fully understand the system, it brings a lot of repetitive code in your refactoring,

The most tragic thing is that the Code after your refactoring is also treated as garbage by others and then reconstructed.

 

So how can we eliminate blindness !?

First, check whether there are problems in the current project, what problems are there, and whether these problems can be solved through reconstruction. If so, you need to rebuild the project, the boss won't rely on youIf the performance is low, you must let him know through the problem. The key is that only through the problem can the time and resources be reconstructed, and your work can be recognized, this is a very realistic situation.

NextYou need to determine whether the reconstructed object is for the architecture or local code, and set an ideal target (Why is it ideal? Because it is impossible for us to achieve this in one step, there is a gap between our ideals and reality,What we need to do is try our best to move closer to the ideal).

 

If the architecture is restructured, this is not an easy task. Before you start, you need to do the following:

1,A comprehensive understanding of the system's past, including the previous architecture/Technical Background and business needs

2,Analyze previous architecture problems, such as: Low maintainability, which aspects do not meet the existing requirements, etc.

3. view at least80%Core code, it is best to have a certain amount of time of real coding experience on the basis of previous Code

The above is to ensure that you have a clear understanding of yourself and know yourself. Can we enter the substantive stage? No, it's a very important thing missing,Refactoring plan!

 

In actual situations, the time generally given by the boss is very different from the time actually needed for restructuring. Therefore, the reconstruction work needs to be delayed in the future, then there will be reconstruction and newDemand development; this work is not a single person, but a group. since it involves multi-person cooperation, in addition to the common goal, a certain review mechanism is also required, this is to ensure that the reconstruction direction is consistent, and so on.Under these factors, reconstruction is the reason for restructuring the plan.

 

Refactoring for local code may be much simpler, but it should be noted that you must consider it within the scope of the existing architecture.

In fact, most of the reconstruction in this way is the extraction method, or the current Code Execution Process is reconstructed based on the idea of a real business process, so that it is easy to understand, or reduce the dependencies between programs. There isImportant ways of thinking:

1Good at analyzing what is the essence of things and what is the external environment of things from a thing.

2Discover commonalities from many different things and abstract these commonalities (For example, for BMW and Audi, You Should abstract them into cars.).

Why do we say this, because these can bring about the need to refactor the Code:

1Reduced dependencies during code writing,

2Abstract things are more reusable.

 

If you do all of the above, is the reconstruction completed! Impossible. This is just a good start. We need to achieve continuous refactoring, as mentioned in agile.

Some people may think it is unrealistic, because the project manager will not give this time in each version cycle. In fact, I am wondering, why not give it ?! The reason for not giving is that if you expect a week or moreFor a long time, no one will agree. The design of a version can be basically completed in a week, and reconstruction takes so long. If you really need it, it means that your preliminary design is poor! The expected time is about two days, becauseThis is limited to small changes.

If you do this well, your project's maintainability will be very good, and joining your project will be a pleasant thing. This is not an ideal thing, as long as you do it persistently, it is actually very easy to implement.

 

These are my personal ideas about refactoring. Maybe the text looks empty, and I will share some real reconstruction cases for your reference and communication.

 

Architecture reconstruction:

1, Restructuring plan

Code reconstruction:

2,ExtractAlgorithm

3,Simple and flexible object Replication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.