继续走这重构一书的复习之路,接着便是内联,除了内联变量,当然还有内联函数。
重构之内联函数
快捷键
Mac: alt
+command
+M
Windows/Linux: Ctrl
+Alt
+M
鼠标: 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
其他
在Intellij IDEA中对于内联函数的使用可能会更简单些,因为它据说是很智能的。
代码
或许您还需要下面的文章: