1. 程式人生 > >Extract Method 提煉函式

Extract Method 提煉函式

  • 你有一段程式碼可以被組織在一起並獨立出來,將這段程式碼放進一個獨立函式中,並讓函式名稱解釋該函式的用途。
void printOwing(double amout) {
    printBanner();

    System.out.println("name:"+_name);
    System.out.println("amount:"+amout);
}
void printOwing(double amount) {
    printBanner();
    printDetails(amount);
}
	
void printDetails(double amount) {
    System.out.println("name:"+_name);
    System.out.println("amount:"+amout);
}
  • 看見一個過長的函式或者一段註釋才能讓人理解用途的程式碼,可以將這段程式碼放進一個獨立的函式中函式命名簡短,名字清晰一目瞭然, 如果每個函式的粒度都很小,那麼函式被複用的機會就更大,其次,這會使高層函式讀起來就像一系列註釋,再次 如果函式都是細粒度,那麼函式的覆寫也會更容易些。
  • 範例:無區域性變數
void pringOwing() {
    Enumeration enumeration =_orders.elements();
    double outstanding = 0.0;
		
    //print banner
    System.out.println("**************************************");
    System.out.println("*********** Customer Owes **********");
    System.out.println("**************************************");
		
    while(e.hasMoreElements()) {
	    _orders each = e.nextElement();
	    outstanding += each.getAmount();
    }
		
    System.out.println("name:"+_name);
    System.out.println("amount:"+outstanding);
	
}
void pringOwing() {
    Enumeration enumeration =_orders.elements();
    double outstanding = 0.0;
		
    printBanner();
		
    while(e.hasMoreElements()) {
	    _orders each = e.nextElement();
	    outstanding += each.getAmount();
    }
		
    System.out.println("name:"+_name);
    System.out.println("amount:"+outstanding);
	
}

void printBanner(){
    //print banner
    System.out.println("**************************************");
    System.out.println("*********** Customer Owes **********");
    System.out.println("**************************************");

}
  • 範例:有區域性變數
void pringOwing() {
    Enumeration enumeration =_orders.elements();
    double outstanding = 0.0;
		
    printBanner();
		
    while(e.hasMoreElements()) {
	    _orders each = e.nextElement();
	    outstanding += each.getAmount();
    }
		
    printDetails(outstanding);
	
}

void printBanner(){
    //print banner
    System.out.println("**************************************");
    System.out.println("*********** Customer Owes **********");
    System.out.println("**************************************");

}

void printDetails(double outstanding){
    System.out.println("name:"+_name);
    System.out.println("amount:"+outstanding);
}

範例:對區域性變數再賦值

void pringOwing() {

    double outstanding = geOutstanding();	

    printBanner();	

    printDetails(outstanding);
	
}

void printBanner(){
    //print banner
    System.out.println("**************************************");
    System.out.println("*********** Customer Owes **********");
    System.out.println("**************************************");

}

void printDetails(double outstanding){
    System.out.println("name:"+_name);
    System.out.println("amount:"+outstanding);
}

double geOutstanding(){
    Enumeration enumeration =_orders.elements();
    double result= 0.0;
    while(e.hasMoreElements()) {
	_orders each = e.nextElement();
	result+= each.getAmount();
    }
    return result;
}