본문 바로가기

Spring

리팩토링

리팩토링이란?

  • 새로운 소스코드를 만들어 내는 것이 아니다.
  • 외부 동작은 변경하지 않고, 내부 구조를 변경하는 작업이다.
  • 코드의 가독성을 좋게 한다.
  • 개발자들마다 결과가 다를 수 있다.

리팩토링 주의사항

  • 기능 추가 구현과 동시에 리팩토링을 하는것을 권장하지 않는다.
  • 기능을 추가하기 전, 리팩토링을 먼저 하는 것을 추천

리팩토링 팁

아래 코드를 리팩토링하면서 리팩토링 팁을 알아보겠습니다.

public class Main {
    public static int splitAndSum(String text) {
        int result = 0;

        if (text == null || text.isEmpty()) {
            result = 0;
        }
        else {
            String[] values = text.split("-");
            for (String value : values) {
                result += Integer.parseInt(value);
            }
        }
        return result;
    }

    public static void main(String[] args) {

        int ret = splitAndSum("11-22-33");

        System.out.println(ret);

    }
}

1. 한 단계의 들여쓰기를 한다.

  • 한 단계 더 들여쓰기를 할때 함수로 빼서 들여쓰기를 없앤다.
...
else {
    String[] values = text.split("-");
    for (String value : values) {
        result += Integer.parseInt(value);
    }
}
...
...
else {
    String[] values = text.split("-");
    result += getSum(values);
}
...

public static int getSum(String[] values) {
    int sum = 0;
    for (String value : values) {
        sum += Integer.parseInt(value);
    }
    return sum;
}

2. else를 없앤다.

  • return 을 사용하여, 필요없는 else를 지운다.
...
if (text == null || text.isEmpty()) {
    result = 0;
}
else {
    String[] values = text.split("-");
    result += getSum(values);
}
...
...
if (text == null || text.isEmpty()) {
    return 0;
}

String[] values = text.split("-");
result += getSum(values);
...

3. 하나의 역할을 하는 메소드로 만든다.

...
public static int splitAndSum(String text) {
    int result = 0;
	
    if (text == null || text.isEmpty()) {
        return 0;
    }
    else {
        String[] values = text.split("-");
        result += getSum(values);
    }
    return result;
}
...

// 현재 getSum 메소드는 String 타입을 int로 바꿔주는 역할과 덧셈까지 수행하고 있다.
public static int getSum(String[] values) {
    int sum = 0;
    for (String value : values) {
        sum += Integer.parseInt(value);
    }
    return sum;
}
...
public static int splitAndSum(String text) {
    int result = 0;
	
    if (text == null || text.isEmpty()) {
        return 0;
    }

    String[] values = text.split("-");
    int[] numbers = toInts(values);
    result += getSum(numbers);

    return result;
}
...

// String 타입을 int로 바꿔주는 메소드
public static int[] toInts(String[] values) {
    int[] numbers = new int[values.length];

    for (int i = 0; i < values.length; i++) {
        numbers[i] = Integer.parseInt(values[i]);
    }

    return numbers;
}

// int로 바꾼 숫자를 더하는 메소드
public static int getSum(int[] numbers) {
    int sum = 0;
    for (int number : numbers) {
        sum += number;
    }
    return sum;
}

4. 임시변수를 제거한다.

  • 의미 파악에 도움이 되지 않는 변수를 제거한다.
...
int result = 0;
	
if (text == null || text.isEmpty()) {
    return 0;
}

String[] values = text.split("-");
int[] numbers = toInts(values);
result += getSum(numbers);

return result;
...
...
if (text == null || text.isEmpty()) {
    return 0;
}

return getSum(toInts(text.split("-")));
...

5. 추상화 Level을 맞춘다.

...
public static int splitAndSum(String text) {
    if (text == null || text.isEmpty()) {
        return 0;
    }

    return getSum(toInts(text.split("-")));
}
...
...
public static int splitAndSum(String text) {
    if (isEmpty(text)) return 0;

    return getSum(toInts(text.split("-")));
}
...

public static boolean isEmpty(String text) {
    if (text == null) return true;
    return text.isEmpty();
}

6. 최종 코드

public class Main {
    public static int splitAndSum(String text) {
        if (isEmpty(text)) return 0;

        return getSum(toInts(text.split("-")));
    }

    public static int[] toInts(String[] values) {
        int[] numbers = new int[values.length];

        for (int i = 0; i < values.length; i++) {
            numbers[i] = Integer.parseInt(values[i]);
        }

        return numbers;
    }

    public static int getSum(int[] numbers) {
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }
        return sum;
    }

    public static boolean isEmpty(String text) {
        if (text == null) return true;
        return text.isEmpty();
    }

    public static void main(String[] args) {

        int ret = splitAndSum("11-22-33");

        System.out.println(ret);

    }
}