가독성을 높이려면 다음과 같이 구현해야 한다.
얼마나 코드가 잘 읽히는지, 코드가 지저분하지 않고 정리된 코드인지를 나타내는 것이 바로 '클린 코드'
public int AAA(int a, int b){
return a+b;
}
public int BBB(int a, int b){
return a-b;
}
두가지 문제점이 있다.
public int sum(int a, int b){
return a+b;
}
public int sub(int a, int b){
return a-b;
}
첫째는 함수 네이밍이다. 다른 사람들이 봐도 무슨 역할을 하는 함수인지 알 수 있는 이름을 사용해야 한다.
둘째는 함수와 함수 사이의 간격이다. 여러 함수가 존재할 때 간격을 나누지 않으면 시작과 끝을 구분하는 것이 매우 어렵다.
ex) 이미 공사가 끝난 집이지만, 더 튼튼하고 멋진 집을 만들기 위해 내부 구조를 개선하는 리모델링 작업
프로젝트가 끝나면, 지저분한 코드를 볼 때 가독성이 떨어지는 부분이 존재한다.
이 부분을 개선시키기 위해 필요한 것이 바로 '리팩토링 기법'
리팩토링 작업은 코드의 가독성을 높이고 향후 이루어질 유지보수에 큰 도움이 된다.
리팩토링의 목적은, 소프트웨어를 더 이해하기 쉽고 수정하기 쉽게 만드는 것
리팩토링은 성능을 최적화시키는 것이 아니다.
코드를 신속하게 개발할 수 있게 만들어주고, 코드 품질을 좋게 만들어준다.
이해하기 쉽고, 수정하기 쉬우면? → 개발 속도가 증가!
명심해야 할 것은, 우선 코드가 제대로 돌아가야 한다는 것.
리팩토링은 우선적으로 해야 할 일이 아님을 명심하자!
객체지향 특징을 살리려면, switch-case 문을 적게 사용해야 함
(switch문은 오버라이드로 다 바꿔버리자)
// 수정 전
public int getFoodPrice(int arg1, int arg2) {
return arg1 * arg2;
}
함수명 직관적 수정, 변수명을 의미에 맞게 수정
// 수정 후
public int getTotalFoodPrice(int price, int quantity) {
return price * quantity;
}
// 수정 전
public int getTotalPrice(int price, int quantity, double discount) {
return (int) ((price * quantity) * (price * quantity) * (discount /100));
}
price * quantity가 중복됨 → 이를 따로 변수로 추출
할인율을 계산하는 부분을 메소드로 따로 추출
할인율 함수 같은 경우는 항상 일정하므로 외부에서 건드리지 못하도록 private 선언
// 수정 후
public int getTotalFoodPrice(int price, int quantity, double discount) {
int totalPriceQuantity = price * quantity;
return (int) (totalPriceQuantity - getDiscountPrice(discount, totalPriceQuantity))
}
private double getDiscountPrice(double discount, int totalPriceQuantity) {
return totalPriceQuantity * (discount / 100);
}
위 코드를 한 번 더 리팩토링 해보면?
// 수정 전
public int getTotalFoodPrice(int price, int quantity, double discount) {
int totalPriceQuantity = price * quantity;
return (int) (totalPriceQuantity - getDiscountPrice(discount, totalPriceQuantity))
}
private double getDiscountPrice(double discount, int totalPriceQuantity) {
return totalPriceQuantity * (discount / 100);
}
totalPriceQuantity를 getter 메소드로 추출할 수 있음
지불한다는 의미를 주기 위해 메소드명 수정
// 수정 후
public int getFoodPriceToPay(int price, int quantity, double discount) {
int totalPriceQuantity = getTotalPriceQuantity(price, quantity);
return (int) (totalPriceQuantity - getDiscountPrice(discount, totalPriceQuantity));
}
private double getDiscountPrice(double discount, int totalPriceQuantity) {
return totalPriceQuantity * (discount / 100);
}
private int getTotalPriceQuantity(int price, int quantity) {
return price * quantity;
}
리팩토링이 더 큰 의미를 가진 것 같다.
클린 코드는 단순히 가독성을 높이기 위한 작업으로 이루어져 있다면, 리팩토링은 클린 코드를 포함한 유지보수를 위한 코드 개선이 이루어진다.
클린코드와 같은 부분은 설계부터 잘 이루어져 있는 것이 중요하고, 리팩토링은 결과물이 나온 이후 수정이나 추가 작업이 진행될 때 개선해나가는 것이 올바른 방향이다.
참고 📖