- Intellij Idea 重构
- 提炼函数
- 内联函数
- 查询取代临时变量
Intellij Idea 重构
下面简单地介绍一下,一些可以直接使用 IDE 就能完成的重构。这种重构可以用在日常的工作中,只需要使用 IDE 上的快捷键就可以完成了。
提炼函数
Intellij IDEA 带了一些有意思的快捷键,或者说自己之前不在意这些快捷键的存在。重构作为单独的一个菜单,显然也突显了其功能的重要性,说说提炼函数,或者说提出方法。
快捷键
Mac: alt+command+M
Windows/Linux: Ctrl+Alt+M
鼠标: Refactor | Extract | Method
重构之前
以重构一书代码为例,重构之前的代码
public class extract {private String _name;void printOwing(double amount){printBanner();System.out.println("name:" + _name);System.out.println("amount" + amount);}private void printBanner() {}}
重构
选中
System.out.println("name:" + _name);System.out.println("amount" + amount);
按下上述的快捷键,会弹出下面的对话框

输入
printDetails
那么重构就完成了。
重构之后
IDE 就可以将方法提出来
public class extract {private String _name;void printOwing(double amount){printBanner();printDetails(amount);}private void printDetails(double amount) {System.out.println("name:" + _name);System.out.println("amount" + amount);}private void printBanner() {}}
重构
还有一种就以 Intellij IDEA 的示例为例,这像是在说其的智能。
public class extract {public void method() {int one = 1;int two = 2;int three = one + two;int four = one + three;}}
只是这次要选中的只有一行,
int three = one + two;
以便于其的智能,它便很愉快地告诉你它又找到了一个重复
IDE has detected 1 code fragments in this file that can be replaced with a call to extracted method...
便返回了这样一个结果
public class extract {public void method() {int one = 1;int two = 2;int three = add(one, two);int four = add(one, three);}private int add(int one, int two) {return one + two;}}
然而我们就可以很愉快地继续和它玩耍了。当然这其中还会有一些更复杂的情形,当学会了这一个剩下的也不难了。
内联函数
继续走这重构一书的复习之路,接着便是内联,除了内联变量,当然还有内联函数。
快捷键
Mac: alt+command+N
Windows/Linux: Ctrl+Alt+N
鼠标: Refactor | Inline
重构之前
public class extract {public void method() {int one = 1;int two = 2;int three = add(one, two);int four = add(one, three);}private int add(int one, int two) {return one + two;}}
在add(one,two)很愉快地按上个快捷键吧,就会弹出

再轻轻地回车,Refactor 就这么结束了。。
Intellij Idea 内联临时变量
以书中的代码为例
double basePrice = anOrder.basePrice();return (basePrice > 1000);
同样的,按下Command+alt+N
return (anOrder.basePrice() > 1000);
对于 python 之类的语言也是如此
def inline_method():baseprice = anOrder.basePrice()return baseprice > 1000
查询取代临时变量
快捷键
Mac: 木有
Windows/Linux: 木有
或者: Shift+alt+command+T 再选择 Replace Temp with Query
鼠标: Refactor | Replace Temp with Query
重构之前
过多的临时变量会让我们写出更长的函数,变量不应该太多,以便使功能单一。这也是重构的另外的目的所在,只有函数专注于其功能,才会更容易读懂。
以书中的代码为例
import java.lang.System;public class replaceTemp {public void count() {double basePrice = _quantity * _itemPrice;if (basePrice > 1000) {return basePrice * 0.95;} else {return basePrice * 0.98;}}}
重构
选中basePrice很愉快地拿鼠标点上面的重构

便会返回
import java.lang.System;public class replaceTemp {public void count() {if (basePrice() > 1000) {return basePrice() * 0.95;} else {return basePrice() * 0.98;}}private double basePrice() {return _quantity * _itemPrice;}}
而实际上我们也可以
选中
_quantity * _itemPrice
对其进行
Extrace Method选择
basePrice再Inline Method
在 Intellij IDEA 的文档中对此是这样的例子
public class replaceTemp {public void method() {String str = "str";String aString = returnString().concat(str);System.out.println(aString);}}
接着我们选中aString,再打开重构菜单,或者
Command+Alt+Shift+T 再选中 Replace Temp with Query
便会有下面的结果:
import java.lang.String;public class replaceTemp {public void method() {String str = "str";System.out.println(aString(str));}private String aString(String str) {return returnString().concat(str);}}
