1. 리팩토링 전 ¶
~cpp #include<iostream> using namespace std; int bottle[9]; int noMove[6]; int movedBottle; int input(); void setNotMove(); int minimumMove(); void output(int noMove); void main() { output(minimumMove()); } int input() { int totalBottle = 0; for(int i = 0; i < 9; i++) { cin >> bottle[i]; totalBottle += bottle[i]; cin.get(); } return totalBottle; } void setNotMove() { noMove[0] = bottle[0] + bottle[4] + bottle[8]; noMove[1] = bottle[0] + bottle[5] + bottle[7]; noMove[2] = bottle[1] + bottle[3] + bottle[8]; noMove[3] = bottle[1] + bottle[5] + bottle[6]; noMove[4] = bottle[2] + bottle[3] + bottle[7]; noMove[5] = bottle[2] + bottle[4] + bottle[6]; } int minimumMove() { int MAX = 0; movedBottle = input(); int minimum; setNotMove(); for(int i = 0; i < 6; i++) { if(noMove[i] > MAX) { MAX = noMove[i]; minimum = i; } } movedBottle -= MAX; cout << endl; return minimum; } void output(int noMove) { switch(noMove) { case 0: cout << "BGC "; break; case 1: cout << "BCG "; break; case 2: cout << "GBC "; break; case 3: cout << "GCB "; break; case 4: cout << "CBG "; break; case 5: cout << "CGB "; } cout << movedBottle << endl; }
2. 리팩토링 후 ¶
~cpp #include<iostream> #include<string> using namespace std; #define NumberOfSlots 9 #define NumberOfCases 6 #define NumberOfResultInformations 2 int* input(); int* setNotMove(int *aSlots); int* minimumMove(int *aSlots); void output(int *aInformation); bool isCorrectInput; void main() { isCorrectInput = true; output(minimumMove(input())); } int* input() { int* slots = new int[NumberOfSlots + 1]; int totalBottle = 0; for(int i = 0; i < NumberOfSlots; i++) { if(!(cin >> slots[i])){ isCorrectInput = false; } totalBottle += slots[i]; } char temp; cin.get(temp); if(temp != '\n') isCorrectInput = false; slots[9] = totalBottle; return slots; } int* setNotMove(int *aSlots) { int* noMove = new int[NumberOfCases]; noMove[0] = aSlots[0] + aSlots[4] + aSlots[8]; noMove[1] = aSlots[0] + aSlots[5] + aSlots[7]; noMove[2] = aSlots[1] + aSlots[3] + aSlots[8]; noMove[3] = aSlots[1] + aSlots[5] + aSlots[6]; noMove[4] = aSlots[2] + aSlots[3] + aSlots[7]; noMove[5] = aSlots[2] + aSlots[4] + aSlots[6]; return noMove; } int* minimumMove(int *aSlots) { if(!isCorrectInput){ delete aSlots; int *temp = new int; return temp; } int MAX = 0; int minimumCase; int* pNoMove = setNotMove(aSlots); for(int i = 0; i < NumberOfCases; i++) { if(pNoMove[i] > MAX) { MAX = pNoMove[i]; minimumCase = i; } } aSlots[9] -= MAX; int *resultInformation = new int[NumberOfResultInformations]; resultInformation[0] = minimumCase; resultInformation[1] = aSlots[9]; delete aSlots; delete pNoMove; return resultInformation; } void output(int *aInformation) { if(isCorrectInput) { string colorsCombination[] = {"BGC", "BCG", "GBC", "GCB", "CBG", "CGB"}; cout << colorsCombination[aInformation[0]] << aInformation[1] << endl; } else { cout << "잘못된 입력으로 프로그램을 종료합니다.\n"; } delete aInformation; }
3. 리팩토링의 목적과 결과 ¶
- 전역변수 남발로 인한 공간(memory)낭비 없애기
- 예외처리
- 불분명한 변수명 변경(가독성)
- 속도의 개선이 조금이나마 이루어짐
~cpp import <string> void output(int *aInformation) { if(isCorrectInput) { string colorsCombination[] = {"BGC", "BCG", "GBC", "GCB", "CBG", "CGB"}; cout << colorsCombination[aInformation[0]] << aInformation[1] << endl; } else { cout << "잘못된 입력으로 프로그램을 종료합니다.\n"; } delete aInformation; }
output() 함수도 이런식으로 리펙토링할 수 있다. --재동
오~ 굿 어드바이스요 --강희경