02 Refactoring _ Improving the design of existing code read note two

Source: Internet
Author: User

02 Refactoring _ Improving the design of existing code read note two

At the heart of the book is a complete list of refactorings, each of which introduces the motivations and techniques of an empirical code transformation. Some projects, such as Extract method and move field, may seem obvious, but don't take it lightly, because understanding such techniques is the key to methodically refactoring. The refactoring guidelines presented in this book will help you to modify your code one step at a time, reducing the risk in the process. You'll soon be adding these refactoring guidelines and their names to your development dictionaries, and catchy.

Some things in the book are too absolute, such as to see Switch refactoring, but this is not realistic, such as Android Development, menu onoptionsitemselected, this is certainly not refactoring, for many control Onclicklistener, It is also common to set up a listener and differentiate convenience points by Viewid, especially in adapter GetView, where a onclicklistener for each control generates too many objects. Of course, this is not to say that the refactoring method is ineffective, but that this approach reminds us that when we see this situation, we need to consider carefully whether the current situation needs to be reconstructed, if it is not necessary, it would be good.

But there are some things that are noteworthy, such as the encapsulation set (encapsulate Collection), when the caller requests a collection object of a class, we'd better not return the collection object, but instead return a non-modifiable copy of the set to the image, and add/ The function that removes the data. For example, for Android-developed adapter, we often add methods that return data collections and set data sets for the convenience of adapter, but it is not safe to determine what the caller will do when the collection is obtained. If the caller modifies the contents of this collection, we know nothing about it, and in Android, if the caller gets a adapter list of data and modifies it in a non-UI thread, it will be problematic.

02 Refactoring _ Improving the design of existing code read note two

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.