Blog

Blog

PHODAL

Intellij IDEA 重构——内联函数

继续走这重构一书的复习之路,接着便是内联,除了内联变量,当然还有内联函数。

重构之内联函数

快捷键

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)很愉快地按上个快捷键吧,就会弹出

Inline Method

再轻轻地回车,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中对于内联函数的使用可能会更简单些,因为它据说是很智能的。

代码

https://github.com/gmszone/refactor

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签