Code refactoring Overview

Source: Internet
Author: User

I.Reconstruction principles

1.Definition of refactoring

refactoring ( TERM ): an adjustment to the internal structure of the software , the purpose is not to change " Software-aware behavior " prerequisites , improved comprehensibility , reduce the modification cost .

refactoring ( verb ): using a series of refactoring principles ( method ), unchanged " Software-aware behavior " prerequisites , adjust its structure .

Both definitions emphasize that they do not change. " External behavior of software " Prerequisites , Not changed " Observability of software " That is to say, the modification does not affect external use.Program ( Programmer ), Externally , The behaviors and results of the program have not changed. . Refactoring only adjusts the internal structure of the program , LetCodeEasier to understand , Then it is easier to maintain .

2.Benefits of refactoring

Improved Software Design

Make software easier to understand

Can help you findBug

Improve software development speed

 

3.When to refactor

Cubic rule : Event 3 , Three Restructures . It means , One thing , Just do it for the first time , The second attempt to do similar things will produce resentment. , However , Do similar things for the third time , You should refactor .

Refactoring when adding new features.

ModifyBugTime Reconstruction

Refactoring during code review.

4.Why rebuild is useful

Programs that are hard to modify have the following features::

Unreadable Program,Difficult to modify

Program with repeated logic,Difficult to modify

To add a new line, you must modify the existing code.,Difficult to modify

Programs with complex conditional Logic,Difficult to modify

A good program should be:

Easy to read

All logic is specified only in the unique location

New changes do not endanger existing behaviors.

Express the conditional logic as simple as possible

 

5.Indirect layer and Reconstruction

Value of the existence of the indirect Layer:

Allow logical sharing

Explain intent and implementation separately

Isolate changes

Encode the Berner Logic

However, too many indirect layers may lead to too deep layers of code.,Make the code hard to read.The advantages and disadvantages of adding indirect layers must be weighed..

6.Reconstruction challenges

Relational Database and Object-Oriented Programming:Insert a separation layer between the object model and the database model,This isolates the changes of the two models..You do not need to upgrade the preceding separation layer when upgrading a model..Such a separation layer will increase the complexity of the system..But it can increase flexibility..

Interface modification Problems : Modify released Interfaces , Because released interfaces are for external personnel ( Other companies ) Use , Therefore , Modifying the interface will cause other programs that reference the interface to run without modifying the program. . The best way to modify an interface is to add a new interface. , Let the old interface call the new interface . In this way, the original program does not need to be modified. . Another suggestion for interfaces is to try not to publish interfaces. .

7.When do I not refactor?

Existing programs cannot run,In this case, the program should be rewritten.,Instead of restructuring

The program has reached the final delivery deadline

 

8.Reconstruction and Design

Reconstruction and design are complementary. , The program should be designed first. , After encoding starts , Reconstruction can be used to compensate for the design deficiency. . The design should be moderate. , Without excessive Design . If it is easy to adapt to the needs through restructuring , So you don't have to design too much. , Code reconstruction when demand changes .

9.Reconstruction and Performance

Three methods to improve performance:

time pre-algorithm : budget the program's time spent during design , generally used for real-time systems with extremely high performance requirements . common enterprise applications do not require high performance . as long as it is not too slow .

continuous attention : requires programmers to maintain high system performance at any time . This method has a defect , is the majority of programs 90% All optimization work is hard. , This wastes a lot of time .

Good Decomposition Method:This method does not focus any attention on performance during the development stage.,Till entering the performance optimization stage,Analyze programs with poor performance,Then break down these programs,Programs with poor performance,Optimization.

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1346264

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.