IntelliJ:重构签名以使用参数属性
IntelliJ: Refactor signature to use parameters properties
在 IntelliJ 中是否可以进行这种重构
public class Demo {
public long sum(Model model) {
int a = model.getA();
int b = model.getB();
System.out.println(model.getA());
System.out.println(model.getB());
return (long) a + b;
}
//refactor to
public long sum(int a, int b) {
System.out.println(a);
System.out.println(b);
return (long) a + b;
}
private static class Model {
private int a;
private int b;
private int c;
//getter & boilerplate
}
}
恕我直言,在某些情况下降低复杂性会非常好。
在 Google 上搜索了一段时间并尝试了各种重构对话 - 找不到比 "Change Signature" 更好的东西了。
编辑: 改进示例,使每个参数有多种用法
好问题!
是的,这可以使用 Extract parameter
和 Inline variable
.
的组合
起点
超过 getA()
、right click > Refactor > Extract > Parameter
(或 ctrl + alt + p on Windows).
结果是
对getB()
做同样的事情。
在局部变量上调用内联变量 quick-fix/refactoring (ctrl + alt + n 在 Windows)
欣赏结果并相应地重命名
使用快捷方式和快速修复导航,我可以在 5 秒内完成所有这些工作 ;)
在 IntelliJ 中是否可以进行这种重构
public class Demo {
public long sum(Model model) {
int a = model.getA();
int b = model.getB();
System.out.println(model.getA());
System.out.println(model.getB());
return (long) a + b;
}
//refactor to
public long sum(int a, int b) {
System.out.println(a);
System.out.println(b);
return (long) a + b;
}
private static class Model {
private int a;
private int b;
private int c;
//getter & boilerplate
}
}
恕我直言,在某些情况下降低复杂性会非常好。
在 Google 上搜索了一段时间并尝试了各种重构对话 - 找不到比 "Change Signature" 更好的东西了。
编辑: 改进示例,使每个参数有多种用法
好问题!
是的,这可以使用 Extract parameter
和 Inline variable
.
起点
超过
getA()
、right click > Refactor > Extract > Parameter
(或 ctrl + alt + p on Windows).
结果是
对getB()
做同样的事情。在局部变量上调用内联变量 quick-fix/refactoring (ctrl + alt + n 在 Windows)
欣赏结果并相应地重命名
使用快捷方式和快速修复导航,我可以在 5 秒内完成所有这些工作 ;)