The experience of refactoring--Class attribute first move

Source: Internet
Author: User

Today began to optimize for someone else to write the project, I first put the relationship between the class to think well, began to move the method, corresponding to move the corresponding class attributes, change to the back, found that the more changes, basically changed all the method of refactoring on all completed, but the change is very large, and the middle of the opportunity to test, has been wrong Coupling is too close, can not be step-by-step, completed about half of the appearance, found that do not go on, even if the bite to go wrong may be too big, I did not grasp the reconstruction after the error.

No way, and re-checkout a copy of the code, think, if you move the method, the method contains the attributes, it is necessary to move the property. However, if you move only the properties, you can easily implement the Move property by the following method, and the corresponding method is still in the original position. For example a property of Class A, moved to Class B, the Do () method of Class A operates a. In this case, you can pass a reference to type B to a, move A to Class B, and provide the Set/get method. Then we replace all the places in Class A with B.seta () and replace them with B.geta () to get the value of a, thus realizing the movement of class properties. Complete this step, the whole program is equivalent to the original, can be directly verified. Because there is no way to modify the invocation of the method, and the logic is not complex, the probability of error is much smaller.

 Public class a {  private  T A;    Public  Do () {    new  T ();    System.out.println ("A:" +A.getvalue ());     return   A.getvalue ();  }    } class T {    private  String value;  
Setvalue/getvalue ...}

Results after moving a property

 public  class   A { private      b b;  do   () {B.seta ( new   T ());    System.out.println ( "A:" +b.geta (). GetValue ());   return   B.geta (). GetValue (); }}  class   B { private   T A;  //  Seta/geta ... } 

If you just look at this small example and don't see the benefits of moving a property, but a class in a legacy project contains many more properties that should not be in that class, and many of the methods of that class need to use these properties, so that you can clear the responsibilities of each class by moving the attributes to where they should be. The refactoring behind is a foundation.

The experience of refactoring--Class attribute first move

Related Article

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.