代码重构
Posted chanaichao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码重构相关的知识,希望对你有一定的参考价值。
1.重构
(1)重构变量
- Shift+F6 批量重命名变量(Rename)
-
private String bcd;//光标在bcd处,按Shift+F6,改为lastname
public String getFullName(String abc){//光标在abc处,按Shift+F6,改为firstname
System.out.println("your firstname: "+abc);
System.out.println("your lastname: "+this.bcd);
String fullName=abc+this.bcd;
System.out.println("your fullname: "+fullName);
this.sayHello(abc);
return fullName;
} - 这段代码abc,bcd含义不明,将abc重构为firstname,bcd重构为lastname,提高代码可读性,改为如下
-
private String lastname;
public String getFullName(String firstname){
System.out.println("your firstname: "+firstname);
System.out.println("your lastname: "+this.lastname);
String fullName=firstname+this.lastname;
System.out.println("your fullname: "+fullName);
this.sayHello(firstname);
return fullName;
}
(2)重构方法
- 方法一: Ctrl+F6 改变方法 (change signature)
-
public String getFullName(String firstname){
//
this.sayHello(firstname);//2.此处出现红波浪,手动添加,lastname
//
}
private void sayHello(String firstname) {//1.在此处按Ctrl+F6 ,即出现一个弹框,在里面点+,输入String lastname即可
} - 方法二:Ctrl+Alter自动修改
-
public String getFullName(String firstname){
//
this.sayHello(firstname);//1.此处添加,lastname,按下Alt+Enter,选择Add String as 2nd parameter to method sayHello
//
}
private void sayHello(String firstname) {//2.此处自动变为private void sayHello(String firstname, String lastname)
}
2.抽取
(1)抽取变量
- Ctrl+Alt+v 抽取变量(extract variable),v就相当于variable
-
public void foo(){
getFullname("san");//光标放在san上,Ctrl+Alt+v, replace all 6 occurrebces
getFullname("san");
getFullname("san");
getFullname("san");
getFullname("san");
getFullname("san");
} - 代码多次需要用到“san”变量,我们可以把它抽取出来,变为
-
public void foo(){
String lastname = "san";
getFullname(lastname);
getFullname(lastname);
getFullname(lastname);
getFullname(lastname);
getFullname(lastname);
getFullname(lastname);
}
(2)抽取静态变量
- Ctrl+Alt+c 抽取静态变量(extract constant),c就相当于constant
-
public void foo(){
getFullname("san");//光标放在san上,Ctrl+Alt+c, replace all 6 occurrebces
getFullname("san");
getFullname("san");
getFullname("san");
getFullname("san");
getFullname("san");
} - 代码多次需要用到“san”作为静态变量,我们可以把它抽取出来,变为
-
public static final String LASTNAME = "san";
public void foo(){
getFullname(LASTNAME);
getFullname(LASTNAME);
getFullname(LASTNAME);
getFullname(LASTNAME);
getFullname(LASTNAME);
getFullname(LASTNAME);
}
(3)抽取成员变量
- Ctrl+Alt+f 抽取成员变量(extract field),f就相当于field
-
public void foo(){
getFullname("san");//光标放在san上,Ctrl+Alt+f, replace all 6 occurrebces
getFullname("san");
getFullname("san");
getFullname("san");
getFullname("san");
getFullname("san");
} - 代码多次需要用到“san”作为静态变量,我们可以把它抽取出来,变为
-
private String lastname;
public void foo(){
lastname = "san";
getFullname(lastname);
getFullname(lastname);
getFullname(lastname);
getFullname(lastname);
getFullname(lastname);
getFullname(lastname);
}
(4)抽取方法参数
- Ctrl+Alt+p 抽取方法参数(extract parameter),p就相当于parameter
-
private String firstName;
private String getFullname(String lastName) {
String fullName= this.firstName +","+lastName;//光标放在firstName,Ctrl+Alt+p
return fullName;
}
public void foo(){
getFullname("san");
} - 将firstName它抽取出来,就可以将getFullname()抽取出来成为一个服务,放到别的类里面都可以,变为
-
private String firstName;
private String getFullname(String lastName, String firstName) {
String fullName= firstName +","+lastName;
return fullName;
}
public void foo(){
getFullname("san", this.firstName);
}
(5)抽取函数
- Ctrl+Alt+m 抽取函数(extract method),m就相当于method
-
public void foo(){
step1();
step2();//选中他俩,按Ctrl+Alt+m,在弹框中输入validate
step3();
step4();//选中他俩,按Ctrl+Alt+m,在弹框中输入loadDataFromDBstep5();
step6();//选中他俩,按Ctrl+Alt+m,在弹框中输入calc}
-
public void foo(){
validate();//校验参数
loadDataFromDB();//数据库加载数据
calc();//逻辑运算
}
private void calc() {
step5();
step6();
}
private void loadDataFromDB() {
step3();
step4();
}
private void validate() {
step1();
step2();
}
以上是关于代码重构的主要内容,如果未能解决你的问题,请参考以下文章