Extract Method
๊ทธ๋ฃน์ผ๋ก ํจ๊ป ๋ฌถ์ ์ ์๋ ์ฝ๋ ์กฐ๊ฐ์ด ์์ผ๋ฉด ์ฝ๋์ ๋ชฉ์ ์ด ์ ๋๋ฌ๋๋๋ก ๋ฉ์๋์ ์ด๋ฆ์ ์ง์ด ๋ณ๋์ ๋ฉ์๋๋ก ๋ฝ์๋ธ๋ค.
ํจ๊ณผ
- ๋ฉ์๋๊ฐ ์ ์ชผ๊ฐ์ ธ ์์ ๋ ๋ค๋ฅธ ๋ฉ์๋์์ ์ฌ์ฉ๋ ํ๋ฅ ์ด ๋์์ง๋ค
- ์ถ์ํ Level์ ์ ์ ํ๊ฒ ์ ์งํ ์ ์์ด ๊ฐ๋ ์ฑ์ด ์ข์์ง๋ค.
// Example Code
void printNesoy(String title, int age){
printTableHead();
System.out.println( "Nesoy title : " + title );
System.out.println( "age : " + age );
}
// Refactoring Code
void printNesoy(String title, int age){
printTableHead();
printNesoyDetail(title, age);
}
void printNesoyDetail(String title, int age){
System.out.println( "Nesoy title : " + title );
System.out.println( "age : " + age );
}
Extract Class
๋ ๊ฐ์ ํด๋์ค๊ฐ ํด์ผ ํ ์ผ์ ํ๋์ ํด๋์ค๊ฐ ํ๊ณ ์๋ ๊ฒฝ์ฐ ์๋ก์ด ํด๋์ค๋ฅผ ๋ง๋ค์ด์ ๊ด๋ จ ์๋ ํ๋์ ๋ฉ์๋๋ฅผ ์์ ํด๋์ค์์ ์๋ก์ด ํด๋์ค๋ก ์ฎ๊ฒจ๋ผ.
ํจ๊ณผ
- ํด๋์ค ํ๋์ ํฌ๊ธฐ๊ฐ ์์์ง์ผ๋ก์จ ํด๋์ค์ ์ญํ ์ ์ดํดํ๊ธฐ ์ฝ๋ค.
// Example Code
class Nesoy {
private String title;
private int age;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAge() {
return age + "๋์ด";
}
public void setAge(int age) {
this.age = age;
}
}
// Refactoring Code
class Nesoy {
private String title;
private int age;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}
class AgeWriter {
public String getAgeFormat(int age){
return age + "๋์ด";
}
}
Inline Class
ํด๋์ค๊ฐ ํ๋ ์ผ์ด ๋ง์ง ์์ ๊ฒฝ์ฐ์๋ ๊ทธ ํด๋์ค์ ์๋ ๋ชจ๋ ๋ณ์์ ๋ฉ์๋๋ฅผ ๋ค๋ฅธ ํด๋์ค๋ก ์ฎ๊ธฐ๊ณ ๊ทธ ํด๋์ค๋ฅผ ์ ๊ฑฐํ๋ผ.
ํจ๊ณผ
- ๋๋ฌด ๊น์ ์ถ์ํ Level๋ณด๋จ ์ ์ ํ ์ถ์ํ Level์ ํตํด ๊ฐ๋ ์ฑ์ ๋ํ๋ค.
// Example Code
int getRating() {
return (moreThanFiveLateDeliveries()) ? 2 : 1;
}
boolean moreThanFiveLateDeliveries() {
return _numberOfLateDeliveries > 5;
}
// Refactoring Code
int getRating() {
return (_numberOfLateDeliveries > 5) ? 2 : 1;
}
Move Method
๋ฉ์๋๊ฐ ์์ ์ด ์ ์๋ ํด๋์ค๋ณด๋ค ๋ค๋ฅธ ํด๋์ค์ ๊ธฐ๋ฅ์ ๋ ๋ง์ด ์ฌ์ฉํ๊ณ ์๋ค๋ฉด ์ด ๋ฉ์๋๋ฅผ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๊ณ ์๋ ํด๋์ค์ ๋น์ทํ ๋ชธ์ฒด๋ฅผ ๊ฐ์ง ์๋ก์ด ๋ฉ์๋๋ฅผ ๋ง๋ค์ด๋ผ. ๊ทธ๋ฆฌ๊ณ ์ด์ ๋ฉ์๋๋ ๊ฐ๋จํ ์์์ผ๋ก ๋ฐ๊พธ๊ฑฐ๋ ์์ ํ ์ ๊ฑฐํ๋ผ
ํจ๊ณผ
- ์ญํ ์ ๋ง๋ ํด๋์ค์ ๋ฉ์๋๋ฅผ ์ฎ๊น์ผ๋ก์จ ํด๋์ค ํ์์ ๋ํด ์ดํด๋๋ฅผ ๋ํ ์ ์๋ค.
// Example Code
class Project {
Person[] participants;
}
class Person {
int id;
boolean participate(Project p) {
for(int i=0; i<p.participants.length; i++) {
if (p.participants[i].id == id) return(true);
}
return(false);
}
}
// Refactoring Code
class Project {
Person[] participants;
boolean participate(Person x) {
for(int i=0; i<participants.length; i++) {
if (participants[i].id == x.id) return(true);
}
return(false);
}
}
class Person {
int id;
}
Move Field
ํ๋๊ฐ ์์ ์ด ์ ์๋ ํด๋์ค๋ณด๋ค ๋ค๋ฅธ ํด๋์ค์ ์ํด ๋ ๋ง์ด ์ฌ์ฉ๋๊ณ ์๋ค๋ฉด ํ๊ฒ ํด๋์ค(target class)์ ์๋ก์ด ํ๋๋ฅผ ๋ง๋ค๊ณ ๊ธฐ์กด ํ๋๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ๋ถ๋ถ์ ๋ณ๊ฒฝํ๋ผ
ํจ๊ณผ
Move Method
์ ๋น์ทํ ๋งฅ๋ฝ์ด๋ค.
Introduce Explaining Variable[Extract Variable]
์กฐ๊ฑด์ ๋ชฉ์ ์ ์ฝ๊ธฐ ์ฌ์ด ๋ณ์์ ์ด๋ฆ์ผ๋ก ํํํ๋ค.
ํจ๊ณผ
- ๋ฉ์๋๋ก ๊ตฌํํ๋ ๊ฒ๋ณด๋ค ํจ์ฌ ๊ฐ๋จํ๋ฉฐ ๊ฐ๋ ์ฑ๋ฉด์์ ๊ฐ๋ ฅํ๋ค.
// Example Code
if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
(browser.toUpperCase().indexOf("IE") > -1) &&
wasInitialized() && resize > 0 )
// Refactoring Code
final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;
final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
final boolean wasResized = resize > 0;
if (isMacOs && isIEBrowser && wasInitialized() && wasResized)
Remove Data Value with Object
์ถ๊ฐ์ ์ธ ๋ฐ์ดํฐ๋ ๋์์ด ํ์๋กํ๋ ๋ฐ์ดํฐ ์์ดํ ์ด ์์๋๋ ๋ฐ์ดํฐ ์์ดํ ์ ๊ฐ์ฒด๋ก ๋ฐ๊ฟ๋ผ.
ํจ๊ณผ
- ๋ฐ์ดํฐ์ ๊ด๋ จ๋ ๋ด์ฉ์ ํ๋์ ํด๋์ค๋ก ์์ฑํ ์ ์๋ค.
// Example Code
class Order{
String customer;
}
// Refactoring Code
class Order{
Customer customer;
}
class Customer{
String name;
}
Replace Array with Object
๋ฐฐ์ด ๋์ Object๋ฅผ ์ฌ์ฉํ๋ผ.
ํจ๊ณผ
- ๋ฐฐ์ด๋ก ์ ๊ทผํ์ฌ ๊ฐ์ ์ ๋ ฅํ๋ ๊ฒ์ ์๋ชป ๋ฃ์ ์ํ์ฑ์ด ์๋ค.
- Object๋ฅผ ํตํด ๊ฐ์ ์ ๋ ฅํ๋ฉด ์๋ชป ๋ค์ด๊ฐ ๊ฐ๋ฅ์ฑ์ ์์จ ์ ์๋ค.
// Example Code
String[] row = new String[3];
row [0] = "Liverpool";
row [1] = "15"
// Refactoring Code
Performance row = new Performance();
row.setName("Liverpool");
row.setWins("15");
Replace Magic Number with Symbolic Constant
Magic Number๋ฅผ ์์๋ก ๋ฐ๊พธ์.
ํจ๊ณผ
- Magic Number๋ ๋ฌด์์ ์๋ฏธํ๋์ง ์ดํดํ๊ธฐ ํ๋ค๋ค. ํ์ง๋ง ์์๋ก ํํํ๋ฉด ์ดํดํ๊ธฐ ์ฝ๋ค.
- Magic Number๋ ๋ณ๊ฒฝํ๊ธฐ๋ ์ด๋ ต๋ค. ํ์ง๋ง ์์๋ก ๊ด๋ฆฌํ๋ฉด ๋ณ๊ฒฝํ๊ธฐ ์ฝ๋ค.
// Example Code
double potentialEnergy(double mass, double height) {
return mass * height * 9.81;
}
// Refactoring Code
static final double GRAVITATIONAL_CONSTANT = 9.81;
double potentialEnergy(double mass, double height) {
return mass * height * GRAVITATIONAL_CONSTANT;
}
Encapsulate Collection
Collection์ ์ง์ ๋ฐํํ์ง๋ง๋ผ.
ํจ๊ณผ
- ๋ณ๊ฒฝ๋ ์ ์๋ ์ฝ์ ์ ๋ํด ๋ณดํธํ ์ ์๋ค.
- ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ๋ ์์ ์ ์์ด์ผ ํ๋ค.
// Example Code
getCourses(): Set
setCourses(:Set)
// Refactoring Code
getCourses(): UnmodifiableSet
addCourse(:Course)
removeCourse(:Course)
Rename Method
๋ฉ์๋์ ์ด๋ฆ์ ์ ์ ํ๊ฒ ๋ฐ๊ฟ๋ผ.
Introduce Parameter Object
๋ง์ ๋งค๊ฐ๋ณ์๋ฅผ ํ๋์ ๋งค๊ฐ๋ณ์ ๊ฐ์ฒด๋ก ๋ฐ๊พธ์ด๋ผ.
ํจ๊ณผ
- ์ฝ๊ธฐ ๋ ํธํ๋ค.
// Example Code
amountInvoicedIn (start : Date, end : Date)
amountReceivedIn (start : Date, end : Date)
amountOverdueIn (start : Date, end : Date)
// Refactoring Code
amountInvoicedIn (: DateRange)
amountReceivedIn (: DateRange)
amountOverdueIn (: DateRange)
Remove Setting Method
ํ์์๋ setter๋ ์ ๊ฑฐํด๋ผ.
Replace Constructor with Factory Method
์์ฑ์ ๋์ Factory Method๋ฅผ ์ฌ์ฉํด๋ผ.
// Example Code
class Employee {
Employee(int type) {
this.type = type;
}
//...
}
// Refactoring Code
class Employee {
static Employee create(int type) {
employee = new Employee(type);
// do some heavy lifting.
return employee;
}
//...
}
Pull Up/Down Field / Pull Up/Down Method
๊ณตํต์ผ๋ก ์ฐ๋ ํ๋,๋ฉ์๋๋ ์์ ํด๋์ค๋ก ํน์ ํ ํ๋,๋ฉ์๋๋ ํน์ ํ ํด๋์ค๋ก
Field
- Pull Up : https://refactoring.com/catalog/pullUpField.html
- Pull Down : https://refactoring.com/catalog/pushDownField.html
Method
- Pull Up : https://refactoring.com/catalog/pullUpMethod.html
- PUll Down : https://refactoring.com/catalog/pushDownMethod.html
Extract Subclass/Superclass
ํน์ง๋ค์ ์/ํ์ ํด๋์ค๋ก ๋ถ๋ฆฌํ๋ผ.
- Subclass : https://refactoring.com/catalog/extractSubclass.html
- Superclass : https://refactoring.com/catalog/extractSuperclass.html
Extract Interface
์ธํฐํ์ด์ค๋ก ๋ถ๋ฆฌํ๋ผ.
Split Temporary Variable
Motivate
- ์์ ๋ณ์๋ ๋ฐ๋ณตํ์ ๋ค์ํ๊ฒ ์ฐ์ผ ์ ์์ง๋ง ํ๋์ ๋ชฉ์ ์ ๋ง๊ฒ ๋ณ์๋ ์กด์ฌํด์ผ ํ๋ค.
- ex) for loop์ i๋ณ์
- ๋ ๊ฐ์ง ์ฉ๋๋ก ์ฌ์ฉํ๋ฉด ์ฝ๋๋ฅผ ๋ณด๋ ์ฌ๋์ด ๋งค์ฐ ํผ๋์ค๋ฌ์ธ ์ ์๋ค.
// Example Code
double temp = 2 * (height + width);
System.out.println(temp);
temp = height * width;
System.out.println(temp);
// Refactoring Code
double perimeter = 2 * (height + width);
System.out.println(perimeter);
double area = height * width;
System.out.println(area);
Hide Delegate
- ์บก์ํ๋ ๊ฐ์ฒด์์ ๊ฐ์ฅ ์ค์ํ ๊ฐ๋ ๊ฐ์ด๋ฐ ํ๋์ด๋ค.
- ์์คํ ์ ๋ค๋ฅธ ๋ถ๋ถ์ ๋ํด ์ข ์ ๊ฒ ์์๋ ๋๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
- ์บก์ํ๊ฐ ๋์ด ์๋ ๊ฒฝ์ฐ ํด๋์ค๊ฐ ๋ณ๊ฒฝ๋์์ ๋ ์์คํ ์ ๋ค๋ฅธ ๋ถ๋ถ์ด ์ํฅ์ ๋ ๋ฐ์ผ๋ฏ๋ก, ๊ฒฐ๊ณผ์ ์ผ๋ก ๋ณ๊ฒฝ์ ์ข ๋ ์ฝ๊ฒ ํ ์ ์๋ค.