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