네이밍이 잘 되어야 함
⇒ 다른 사람이 봐도 무슨 역할을 하는 함수인지 알 수 있게
오류가 없어야 함
중복이 없어야 함
의존성을 최대한 줄여야 함
클래스, 메소드가 한 가지 일만 처리 해야 함
프로그램의 외부 동작은 그대로 둔 채, 내부의 코드를 정리하며 개선하는 것
리팩토링 작업을 함으로써 가독성을 높이고, 유지보수에 도움 줌
리팩토링은 성능을 최적화 시키는 것 X
소프트웨어를 더 이해하기 쉽고, 수정하기 쉽게 만드는 것
중복 코드
긴 메소드
거대한 클래스
Switch문
절차지향으로 구현한 코드
절차지향 VS 객체지향
절차지향 프로그래밍
순차적인 처리 중시
장점
: 컴퓨터의 작업 처리 방식과 유사 ⇒ 객체지향 언어보다 실행 속도 빠름
단점
1) 유지보수 어려움
2) 실행 순서가 정해져 있으므로 코드의 순서가 바뀌면 동일한 결과 보장하기 어려움
3) 디버깅이 어려움
Ex) C언어
객체지향 프로그래밍
실제 세계를 모델링하여 소프트웨어를 개발하는 방법
특징
1) 추상화
2) 캡슐화
3) 상속
4) 다형성
장점
1) 코드의 재활용성 높음
2) 코딩이 절차지향보다 간편함
3) 디버깅이 쉬움
단점
1) 처리속도가 절차지향에 비해 느림
2) 설계에 많은 시간이 소요 됨
// 수정 전
public int getTotalPrice(int price, int quantity, double discount) {
return (int) ((price * quantity) * (price * quantity) * (discount /100));
}
'price * quantity'가 중복 됨
⇒ 중복 코드를 줄이기 위해, 따로 변수(totalPriceQuantity)로 추출
할인율 계산하는 부분을 메소드로 따로 추출
// 1차 수정 후
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);
}
// 2차 수정 후
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;
}