Rebuilding like a craftsman-refactoring workbook

Source: Internet
Author: User

Author: Jiangnan Baiyi

For the latest version and comments, see: http://www.blogjava.net/calvin/archive/2005/10/04/14790.html

Restructuring like a Craftsman makes refactoring a craft.
Martin Fowler's refactoring is actually more suitable for a brainwashing and declarative book about refactoring, just as thin as Kent Beck's XP explain. It is a pity that he is very thick, and the Directory reconstruction is written to Xiao Bai. Therefore, I prefer refacoring workbook, which teaches refactoring techniques in the tone of a craftsman.

1. refactor between classes  <Design Pattern> half of the length of this article is to educate you not only on inheritance, but also on combination/delegation. There are actually many things in refactoring to be solved by turning inheritance into delegation. 1 . 1 Inheritance
1.1.1 parallel inheritance system, combination explosion
This was a headache in the past and is now used to entrusting.
In addition, Java also has an unsatisfactory interface mechanism to solve parallel inheritance.
1.1.2 If the relationship between parent and child classes is too close, the Child classes will modify the methods of the parent class and access the variables of the parent class. At this time, the final template method can be defined. There are also rejected gifts. I haven't encountered any problems yet. The author also suggests that you leave it alone if nothing happens. If something happens, it will be difficult to move the method; or The subclass does not inherit the parent class, instead, it only combines the parent class. 1 . 2. Responsibilities
After many restructures, I found that which method should be put in which class is actually subjective. you can wake up every day to think of a reason for a method to move your house, so now I have abandoned the pursuit of a "right" Responsibility Assignment, just look at it.
1 . 3 modified in the pop-up mode
There is no particular good way to change N classes when making a change. You just need to find out if there are any classes that are responsible for the change that govern the whole world.
However, many of today's pop-up modifications are made in layers.
1 . 4 Library Class
Opensource class library, which sometimes needs to be extended
1. If there are only one or two methods, they can be extended directly in the Customer Code,
2. Otherwise, add a subclass of the class library.
3. The greatest effort is to introduce a new layer.
In other words, refactoring is actually dependent on tools and ownership of all the code. Just click "RENAME" for the entire project. When you design a library class, you do not have Customer CodeSo we have to design it carefully at the beginning, so we can't rely on refactoring. Modifying the interface will make everyone dead.

2. refactor within classes2.1 are sins
Long method, large class, and long parameter list are usually found through the measurement tool. You can also set a trigger to trigger an alarm when the measurement value exceeds a certain limit.
This function can be implemented by PMD, but I prefer metrics reload, an idea plug-in, which provides comprehensive metrics.
However, you should not refactor the data to measure the value.
Of course, long method is to try extract method.
Large Class separates the functions of the class from several fields and tries to split another class or subclass.
Long parameter list can be obtained through the query of variables in the method without passing in parameters;
Or make some parameters into an object.1.2 repetition is also a sin
Repeat is considered a bad thing 30 years ago, as shown in 1. Code is similar, 2. Code and interface are different and have similar functions.
There is nothing special about how to remove duplicates.
Extract Method (same class ). When there is a difference, the sharing is achieved through parameterization.
Pull up method to parent class (same parent class ). When there is a difference, the template mechanism is used for sharing.
Class A calls Class B or extract class C (two completely unrelated classes)1.3 name
Chinese programmers are inherently inferior in English. For more information, see the naming of OFBiz and comperie to create a team's project dictionary and domain term dictionary as soon as possible. Fortunately, with the help of tools, code rename is the easiest way to refactor.
1.4 complex conditional expressions

The author believes that even though program is most concerned with objects and the relationship between objects, high-quality internal code is still important. We recommend programming Pearl and elements of Programing style.

There are three methods to simplify complex conditions.
1. Pass! (A & B) = (! A) | (! B) Simplification
2. Replace conditional expressions with meaningful variables, such
Boolean ismidscore = (x> 1000) & (x <3000 );
3. Put guard clause in front by splitting an if statement.
For example, if (a | B)
Do ();
-> If ()
Do ();
If (B)
Do ();
2 and 3 can be flexibly combined. For example, an isright () function is generated based on 2 and extract.

    isRight()
    {
      if(A)
        return true;
      if(B)
        return true;
      return false;
    }

1.5 others
Useless dead code. Use the IDE tool to discover and remove it. Be careful that some Framework Code cannot be deleted.
Magic number, of course, is changed to a constant. If there are many magic numbers, you can use map and enumeration classes to store them.
The comment of the deodorant type is a method, and the variable is changed to a more suitable name. If the annotation is for a code segment, you can use the extract method. Of course, the code can only describe how, not why, or why not. This is where the annotation exists.

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.