refactoring tools使用指南
Refactoring
一、Encapsulate Field (封装值域)
There is a public field.
Make it private and provide accessors.
public String _name
private String _name;
public String getName() {return _name;}
public void setName(String arg) {_name = arg;}
二、Extract Method (提炼函数)
You have a code fragment that can be grouped together.
Turn the fragment into a method whose name explains the purpose of the method.
void printOwing() {
printBanner();
//print details
System.out.println ("name: " + _name);
System.out.println ("amount " + getOutstanding());
}
void printOwing() {
printBanner();
printDetails(getOutstanding());
}
void printDetails (double outstanding) {
System.out.println ("name: " + _name);
System.out.println ("amount " + outstanding);
}
Extract Method
By simply selecting a block of code to extract and select the menu item "Extract Method", the tool extract the code from the original method and make a new method and replaces extracted code with a call to new method.
example
* We remake C# source code from Java example in Refactoring book
1. Select code to extract
2. Select "Extract Method" menu
3. Enter new method name
三、Inline Temp (将临时变量内联化)
You have a temp that is assigned to once with a simple expression, and the temp is getting in the way of other refactorings.
Replace all references to that temp with the expression.
double basePrice = anOrder.basePrice();
return (basePrice > 1000)
return (anOrder.basePrice() > 1000)
四、Introduce Explaining Variable (引入解释性变量)
You have a complicated expression.
Put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose.
if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
(browser.toUpperCase().indexOf("IE") > -1) &&
wasInitialized() && resize > 0 )
{
// do something
}
final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;
final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
final boolean wasResized = resize > 0;
if (isMacOs && isIEBrowser && wasInitialized() && wasResized)
{
// do something
}
Introduce Explaining Variable
If you have a complicated expression, by selecting that expression and select the menu item "Introduce Explaining Variable", the tool replace expression with the variable and add variable declaration.
example
* We remake C# source code from Java example in Refactoring book
1. Select expression to extract
2. Select "Introduce Explaining Variable" menu item
3. Enter new variable name
4. Result
五、Move Method (搬移函数)
A method is, or will be, using or used by more features of another class than the class on which it is defined.
Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether.
For more information see page 142 of Refactoring
Additional Comments
Marian Vittek sent an example for moving a method to a method argument.
class Project {
Person[] participants;
}
class Person {
int id;
boolean participate(Project p) {
for(int i=0; i<p.participants.length; i++) {
if (p.participants[i].id == id) return(true);
}
return(false);
}
}
... if (x.participate(p)) ...
After applying the move you end up with
class Project {
Person[] participants;
boolean participate(Person x) {
for(int i=0; i<participants.length; i++) {
if (participants[i].id == x.id) return(true);
}
return(false);
}
}
class Person {
int id;
}
... if (p.participate(x)) ...
He also points out that the part of the Move Method mechanics that reads Determine how to reference the correct target object from the source, should be replaced by Determine how to reference the correct target object from the source or from arguments of the method which is more general.
六、Rename Local Variable (重命名本地变量)
By selecting a local variable name that declared in a method and select the menu item "Rename Local Variable", the tool replace the local variable name in method to user designated name.
example
1. Select variable to rename
2. Select "Rename Local Variable" menu item
3. Enter new variable name
4. Result
七、Replace Magic Number with Symbolic Constant (以符号常量/字面常量取代魔法数)
You have a literal number with a particular meaning.
Create a constant, name it after the meaning, and replace the number with it.
double potentialEnergy(double mass, double height) {
return mass * height * 9.81;
}
double potentialEnergy(double mass, double height) {
return mass * GRAVITATIONAL_CONSTANT * height;
}
static final double GRAVITATIONAL_CONSTANT = 9.81;
For more information see page 204 of Refactoring
Additional Comments
Using a constant method
For this refactoring I used a symbolic constant, which is the most common Java idiom.
However an alternative is the constant method, which is a method of this form
public static double gravitationalConstant() {
return 9.81;
}
This idiom is less familiar to C based programmers, but is very familiar to Smalltalkers (who didn't have constants in their language). On the whole I don't tend to use this in Java as it is less idiomatic to the language. However if you need to replace the simple return with a calculated value then it's worth changing the constant field to a constant method. (I guess there should be a refactoring for that....)
Replace Magic Literal with Symbolic Constant
By selecting a literal and select the menu item "Replace Magic Literal with Symbolic Constant", the tool replace the literal with a constant.
example
* We remake C# source code from Java example in Refactoring book
1. Select literal to replace
2. Select "Replace Magic Literal with Symbolic Constant" menu item
3. Enter new constant name
4. Result
八、Replace Temp with Query (以查询代替临时变量)
You are using a temporary variable to hold the result of an expression.
Extract the expression into a method. Replace all references to the temp with the expression. The new method can then be used in other methods.
double basePrice = _quantity * _itemPrice;
if (basePrice > 1000)
return basePrice * 0.95;
else
return basePrice * 0.98;
if (basePrice() > 1000)
return basePrice() * 0.95;
else
return basePrice() * 0.98;
...
double basePrice() {
return _quantity * _itemPrice;
}
Replace Temp with Query
If you are using a local variable(temp) that is assigned to once with a expression, by selecting that name and select the menu item "Replace Temp with Query", the tool make new method using expression and replace variable with the new method call and remove variable declaration.
example
* We remake C# source code from Java example in Refactoring book
1. Select variable to replace
2. Select "Replace Temp with Query" menu item
3. Enter new method name
4. Result
posted on 2004-04-24 13:41 十一月四日风雨大作 阅读(1601) 评论(5) 编辑 收藏 举报