본문 바로가기

Refactoring

[ Refactoring ] Split Temporary Variable 루프 안에 있는 변수나 collecting temporary variable도 아닌 임시 변수에 값을 여러 번 대입하는 경우에는, 각각의 대입에 대해서 따로따로 임시변수를 만들어라. double temp = 2 * (_height + _width); System.out.println (temp); temp = _height * _width; System.out.println (temp); final double perimeter = 2 * (_height + _width); System.out.println (perimeter); final double area = _height * _width; System.out.println (area); * collecting temporary variable =.. 더보기
[ Refactoring ] Introduce Explaing Variable 복잡한 수식이 있는 경우에는, 수식의 결과나 또는 수식의 일부에 자신의 목적을 잘 설명하는 이름으로 된 임시변수를 사용하라. if ( (platform.toUpperCase().indexOf("MAC") > -1) && (browser.toUpperCase().indexOf("IE") > -1) && wasInitialized() && resize > 0 ) { // do something } final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1; final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1; final boolean wasResized = resize > 0;.. 더보기
[ Refactoring ] Replace Temp with Query Method 어떤 수식의 결과값을 저장하기 위해서 임시변수를 사용하고 있다면, 수식을 뽑아내서 메소드로 만들고, 임시변수를 참조하는 곳을 찾아 모두 메소드 호출로 바꾼다. 새로 만든 메소드는 다른 메소드에서도 사용될 수 있다. double basePrice = _quantity * _itemPrice; if (basePrice > 1000) return basePrice * 0.95; else return basePrice * 0.98; if (basePrice() > 1000) return basePrice() * 0.95; else return basePrice() * 0.98; ... double basePrice() { return _quantity * _itemPrice; } 동기 임시변수는 그것이 사용되는.. 더보기
[ Refactoring ] Inline Temp 간단한 수식의 결과값을 가지는 임시 변수가 있고, 그 임시 변수가 다른 리팩토링을 하는데 방해가 된다면 이 임시변수를 참조하는 부분을 모두 원래의 수식으로 바꿔라 double basePrice = anOrder.basePrice(); return (basePrice > 1000) return (anOrder.basePrice() > 1000) 동기 대부분의 경우 Inline Temp는 Replace Temp with Query의 한 부분으로 사용된다. 진짜 동기는 그쪽에 있다. Inline Temp가 자신의 목적으로 사용되는 유일한 경우 메소드 호출의 결과값이 임시 변수에 대입되는 경우 임시 변수가 Extract Method와 같은 리팩토링에 방해가 된다면 인라인화 하라 절차 임시 변수를 final로 선.. 더보기
[ Refactoring] Inline Method 메소드 몸체가 메소드의 이름만큼이나 명확할때는 호출하는 곳에 메소드의 몸체를 넣고, 메소드를 삭제하라 int getRating() { return (moreThanFiveLateDeliveries()) ? 2 : 1; } boolean moreThanFiveLateDeliveries() { return _numberOfLateDeliveries > 5; } int getRating() { return (_numberOfLateDeliveries > 5) ? 2 : 1; } 동기 이럴때는 메소드를 제거한다. 때로는 메소드의 몸체가 메소드의 이름만큼이나 명확할때가 있다. 메소드의 몸체를 메소드의 이름만큼이나 명확하게 리팩토링 할수도 있다. 필요없는 인디렉션은 짜증나게 한다 메소드가 잘못 나뉘어졌을때 메소드를.. 더보기
[Refactoring] Extract Method 그룹으로 함께 묶을 수 있는 코드 조각이 있으면 코드의 목적이 잘 드러나도록 메소드의 이름을 지어 별도의 메소드로 뽑아낸다. void printOwing(double amount) { printBanner(); //print details System.out.println ("name:" + _name); System.out.println ("amount" + amount); } void printOwing(double amount) { printBanner(); printDetails(amount); } void printDetails (double amount) { System.out.println ("name:" + _name); System.out.println ("amount" + amount).. 더보기