refactoring tools使用指南

Refactoring<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

一、Encapsulate Field (封装值域)

There is a public field.

Make it private and provide accessors.

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

二、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.

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.

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.

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

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.

After applying the move you end up with

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
 

You have a literal number with a particular meaning.

Create a constant, name it after the meaning, and replace the number with it.

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

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
 

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.

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

转载于:https://www.cnblogs.com/jiangyu/archive/2004/04/24/7461.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值