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

Method

Extract Subclass/Superclass

ํŠน์ง•๋“ค์„ ์ƒ/ํ•˜์œ„ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•˜๋ผ.

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

  • ์บก์Šํ™”๋Š” ๊ฐ์ฒด์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฐœ๋… ๊ฐ€์šด๋ฐ ํ•˜๋‚˜์ด๋‹ค.
  • ์‹œ์Šคํ…œ์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ข€ ์ ๊ฒŒ ์•Œ์•„๋„ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
  • ์บก์Šํ™”๊ฐ€ ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ํด๋ž˜์Šค๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ์‹œ์Šคํ…œ์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์˜ํ–ฅ์„ ๋œ ๋ฐ›์œผ๋ฏ€๋กœ, ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ณ€๊ฒฝ์„ ์ข€ ๋” ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Reference