리팩토링(Refactoring)은 소프트웨어의 외부 동작은 변경하지 않으면서 내부 구조를 개선하는 과정을 말합니다. 리팩토링을 통해 코드의 가독성, 유지보수성, 확장성을 높일 수 있습니다. 다음은 대표적인 리팩토링 기법들입니다.
1. 메서드 추출(Extract Method)
긴 메서드나 중복된 코드를 별도의 메서드로 추출하여 코드의 가독성과 재사용성을 높이는 기법입니다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
 | 
 // Before 
public void printOwing() { 
    printBanner(); 
    System.out.println(“name: “ + name); 
    System.out.println(“amount: “ + getOutstanding()); 
} 
// After 
public void printOwing() { 
    printBanner(); 
    printDetails(getOutstanding()); 
} 
public void printDetails(double outstanding) { 
    System.out.println(“name: “ + name); 
    System.out.println(“amount: “ + outstanding); 
} 
 | 
cs | 
2. 변수 추출(Extract Variable)
복잡한 표현식을 변수로 추출하여 코드의 의도를 명확히 하는 기법입니다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
 | 
 // Before 
double basePrice = quantity * itemPrice; 
double discountFactor = 0.95; 
if (basePrice > 1000) { 
    discountFactor –= 0.03; 
} 
// After 
double basePrice = quantity * itemPrice; 
double discountFactor = 0.95; 
if (basePrice > 1000) { 
    discountFactor –= 0.03; 
} 
double finalPrice = basePrice * discountFactor; 
 | 
cs | 
3. 메서드 내용 직접 삽입(Inline Method)
메서드 호출을 메서드 내용으로 대체하여 코드의 복잡성을 줄이는 기법입니다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
 | 
 // Before 
public boolean hasDiscount(Order order) { 
    return order.getPrice() > 100; 
} 
double calculateTotal(Order order) { 
    if (hasDiscount(order)) { 
        return order.getPrice() * 0.9; 
    } else { 
        return order.getPrice(); 
    } 
} 
// After 
double calculateTotal(Order order) { 
    if (order.getPrice() > 100) { 
        return order.getPrice() * 0.9; 
    } else { 
        return order.getPrice(); 
    } 
} 
 | 
cs | 
4. 조건문 분해(Decompose Conditional)
복잡한 조건문을 별도의 메서드로 분해하여 코드의 이해도를 높이는 기법입니다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
 | 
 // Before 
double calculateTotal(Order order) { 
    double total = 0; 
    if (order.getCountry().equals(“US”) && order.getState().equals(“CA”)) { 
        total = order.getPrice() * 1.1; 
    } else { 
        total = order.getPrice(); 
    } 
    return total; 
} 
// After 
double calculateTotal(Order order) { 
    double total = 0; 
    if (isCaliforniaOrder(order)) { 
        total = order.getPrice() * 1.1; 
    } else { 
        total = order.getPrice(); 
    } 
    return total; 
} 
private boolean isCaliforniaOrder(Order order) { 
    return order.getCountry().equals(“US”) && order.getState().equals(“CA”); 
} 
 | 
cs | 
5. 메서드 객체 전환(Replace Method with Method Object)
메서드 내부의 변수와 파라미터가 많아질 경우, 해당 메서드를 별도의 객체로 전환하여 코드의 가독성을 높이는 기법입니다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
 | 
 // Before 
public class Order { 
    // … 
    public double price() { 
        double primaryBasePrice; 
        double secondaryBasePrice; 
        double tertiaryBasePrice; 
        // Perform long computation… 
    } 
} 
// After 
public class Order { 
    // … 
    public double price() { 
        return new PriceCalculator(this).compute(); 
    } 
} 
public class PriceCalculator { 
    private double primaryBasePrice; 
    private double secondaryBasePrice; 
    private double tertiaryBasePrice; 
    public PriceCalculator(Order order) { 
        // Copy relevant information from the order object. 
    } 
    public double compute() { 
        // Perform long computation… 
    } 
} 
 | 
cs | 
6. 클래스 추출(Extract Class)
클래스가 너무 많은 역할을 담당할 경우, 일부 역할을 새로운 클래스로 추출하여 단일 책임 원칙(SRP)을 준수하는 기법입니다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
 | 
 // Before 
public class Person { 
    private String name; 
    private String officeAreaCode; 
    private String officeNumber; 
    // … 
} 
// After 
public class Person { 
    private String name; 
    private TelephoneNumber officeTelephoneNumber; 
    // … 
} 
public class TelephoneNumber { 
    private String areaCode; 
    private String number; 
    // … 
} 
 | 
cs | 
리팩토링은 소프트웨어 개발에서 중요한 실천법입니다. 코드를 지속적으로 개선함으로써 기술 부채를 최소화하고, 변화에 유연하게 대응할 수 있는 코드 베이스를 유지할 수 있습니다. 리팩토링을 할 때는 작은 단계로 진행하고, 각 단계마다 테스트를 수행하여 코드의 정확성을 보장해야 합니다.
- Refactoring: Improving the Design of Existing Code – Martin Fowler
 - Refactoring to Patterns – Joshua Kerievsky
 - Refactoring Guru – Refactoring Techniques
 - SourceMaking – Refactoring
 
이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

			