Author: Jiangnan white
Latest version and comment Please see: http://www.blogjava.net/calvin/archive/2005/104/14790.html
Reconstructing like a craftsman, letting reconciled into a craft. "Refactoring" of Martin Fowler is actually more suitable for the refactorful brainwashing, declarative book, just like Kent Beck's "XP Explain" OK. Unfortunately, he is very thick, and the reconstructed list of the back is written to Xiaobai. So I prefer "Refacoring Workbook" to transfer the reconstructed craftsmanship with a craftsman (silent).
1. Recipient Between Classes
"Design Pattern" There are half of the units that you can't rely on inheritance, and you must use combination / commission. In fact, there are still many things to be resolved by inheriting inheritance.
1
.1 inherited 1.1.1 Parallel inheritance system, combined explosion
This is a problem that is a headache, and now it is used to commissioned.
In addition, Java is not very satisfactory interface mechanism to solve parallel inheritance.
1.1.2 Relationship between father and son
For example, if it is too intimate, the subclass will modify the parent class, access the variable of the parent class, which can define the final Template method.
There is also a refusal gift. I have not encountered problems in this time. The author also recommends that if it is nothing, if there is something, it is necessary to fill the MOVE METHOD; or subclass does not inherit the parent class, but only combined parent class.
1
After many reconstruction, I found that which method should be placed in which class is actually subjective. You can think of a reason for a way to move home every day. So I have given up the pursuit of a way now. "For" duties are allocated, watching it in your eyes.
1
.3 scattered modification
When making a revision, it is necessary to change n class, and there is nothing special. It is to find it. Is there a class that is responsible for this modification.
But now a lot of bullet modifications are made in layers.
1
.4 library class
OpenSource's class library, always want to expand
1. If it is just one or two ways, expand directly in the customer code,
2. Otherwise, one more library subclass
3. The most tough is to introduce a new layer
Excellence, Refact it is actually a tool, and the ownership of all code, it will come to a full project RENAME. When you design the library class, you don't have to have these library classes.
The customer code, so we must carefully design at the beginning, can not rely on reconstruction, and the interface will let people die.
2. Reconstructing within classes
2.1 is a crime
Long Method, LARGE CLASS, Long Parameter List, typically found through the metrics, you can also set a trigger yourself, when the measure value exceeds a certain limit, the alarm.
PMD can implement this feature, but I prefer Metrics Reload, an IDEA plugin, and give the metric information.
However, it should be refactored to measure the value of the measurement.
LONG METHOD is of course trying to extract Method.
Large Class is to separate the functions of the class several domains and try to remove another class or subclass.
Long Parameter List can be obtained by querying in the method without incorporating by parameters;
Or make some parameters into an object.
1.2 Repeat is also a sin
Repeat is considered a bad thing as a bad thing 30 years ago, and the performance is similar to the code, 2. Code, and interface.
There is nothing special, nothing more, nothing more than
Extract Method (Same Class). When there is a difference, sharing is achieved by parameterization.
Pull Up Method to the parent class (the same parent class). When there is a difference, sharing is achieved through the template mechanism. Class A calls Class B or Extract Class C (two completely unable classes)
1.3 Naming Chinese programmers are poor, more references to OFBIZ, Comperie naming, establish team project dictionary, field terms dictionary as soon as possible.
Fortunately, now in tool assistance, the Rename of the code is the easiest refactoring.
1.4 complex conditions expressions
The author believes that even now Program is most concerned about the object, and the relationship between the object, the high quality internal code is still important, recommend "programming beads" and "Elements of Programing Style".
Three ways to simplify the implicit conditions
1. Pass! (A & B) == (! A) || (! B)
2. Through meaningful variables, functions instead of conditions expressions, such as
Boolean ismidscore = (x> 1000) && (x <3000);
3. Put Guard Clause in front by executing an IF split
For example, IF (A || b)
DO ();
-> IF (a)
DO ();
IF (b)
DO ();
It can also combine 2, 3, such as a ISRIGHT () function according to 2, extract, according to 3
ISRIGHT () {if (a) return true; if (b) return true; return false;}
1.5 other
Useless dead code, detect and remove through the IDE tool. Be careful that some framework code cannot be deleted.
Magic Number, of course, is changed to constant. If Magic Number is a lot, you can use MAP and enumerated classes.
Refodicle-based annotations, for methods, variables to change a more suitable name. If the comment is for a code segment, you can extract Method. Of course, the code can only explain how, can't explain why, and you can't explain why not, which is where the comment is present.