码迷,mamicode.com
首页 > 其他好文 > 详细

代码重构

时间:2018-07-02 17:14:28      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:out   sys   重构   omd   重命名   stat   rename   oid   alc   

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,在弹框中输入loadDataFromDB
        step5();
    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();
    }

 

代码重构

标签:out   sys   重构   omd   重命名   stat   rename   oid   alc   

原文地址:https://www.cnblogs.com/chanaichao/p/9254625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!