Refactoring to improve the design of existing code Chapter one reading summary

Source: Internet
Author: User

The first chapter of this book is mainly about a small case of refactoring, which is a good understanding of the personal feeling that it is better than writing a pile of refactoring theories.

This is a film rental case there is a film category (film information), leasing (film and rental days), customer category (Customer name, leasing information, detailed list), a detailed function inside returns a string type of result that records the customer's name, the rental of each film information, The total amount of money to be paid and the points earned by the customer. A function has done so many things, in large projects this is not conducive to the reuse of code and later code maintenance increased difficulties, so this case is mainly to the detailed single function is reconstructed.

It is necessary to build a reliable set of test environments for the code that is about to be modified before refactoring, which must also have the ability to self-detect either to show the output as consistent with the reference results or to list the line numbers where the failure manifest displays the problem, which can help you find the error saving development time to improve the quality of your code.

The first step is to find out the code's logical mud switch and refine the function , before refining the function must be analyzed clearly what may be wrong, if the poor refining can introduce bugs to the program. After you have finished refining the function, compile the test to see if your changes introduce bugs. After you refine the switch, you can also modify the name of the variable inside the function to make it look fresher and easier to understand, but you need to compile the test to make sure nothing is broken. After modifying the feeling of the billing method is not very appropriate in the customer class, so the operation of the move function , the billing method moved to the lease class, and then go to the details of the corresponding switch of the code to the lease class object call billing method. The previous thisamount also becomes redundant because it is equal to the return result of the function so the thisamount is removed by the query instead of the temporary variable , and the test is compiled for each operation. Refactoring is the process of modifying a program at a small pace, which can easily be found if mistakes are made. This has been distilled from the billing code in the details method. The same method is used to reconstruct the code of the frequent-flyer integral calculation part in the lease class. The member variables inside the class are then used as self-encapsulated fields to ensure that they are accessed at any time by value and setting functions.

From this example, we can see the rhythm of Refactoring: testing, minor modification, testing, minor modification, testing, minor modification 、、、, so testing is important for refactoring, which allows refactoring to move quickly and safely.

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.