ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Day01


        ? ?? ?? ??? ??

            ????



                         1
????

??? ?????? ??? ????? ?? ???? ??? ? ??.
??? ??? ???? ??? ??? ??? ???? ??? ??? ?
?? ? ??.
??? ???? ??? ??? ??? ???? ?? ??? ??? ? ?
?.




                                              2
                                          ?????
1. ????


 1. ???? ??

 2. ?? ?? [??]




                     3
                 ?????
1.???? ??




            4
??? Abstraction               ???? ??




                  ??? ??? ??? ???
                  ???? ?? ?? ????
                  ???? ????
                  ???? ?? ??? ??




                                        5
?? Inheritance               ???? ??




                 ?? ???? ?? ?? ??
                 ???? ???? ????
                 ???? ??? ???? ?
                 ? ???? ??




                                    6
??? Capsulation                  ???? ??




                  ??? ??? ?? ????
                  ???, ?? ??? ????
                  ?? ??? ?? ??




                                           7
??? Polymorphism                 ???? ??




 polymorphism      ??? ???? ?? ?? ?


 ¶àÐÎÐÔ               ?? ?? ? ?? ?. ?? ?
                   ?? ?? ??? ??? ??
                   ??
       TYPE

                                        8
2. ?? ??
   [??]




           9
2. ???????


 1. ???? ? ? ??

 2. ???????

 3. ??? ??

 4. ????? ??

 5. ?? ?? ?? ??



                    10
                  ????
1. ???? ? ? ??
 Project in the world




                        11
???? In IDEAL                  Project in the world



 ? ???? ???? ?? ??? ?? ??

 ? ???? ??? ??

 ? ???? ??? ???? ??? ???

 ? ??? ??? ???? ? ?? ??? ???

 ? ??? ??? ???? ?? ?? ?? ??




                                                  12
???? In REAL                             Project in the world



 ? ???? ???? ??
   ¨C ???? ?? ??? ??? Skill? ??? ?? ??.

   ¨C   ??? ??? ?? ????.

   ¨C   ?? ??? ???? ???? ?? ??? ???,

   ¨C   ??? ??? ??? ??? ??.

   ¨C   ???? ???? ?? ???? ????

   ¨C ?? ??? ??? ???. ????? ? ???.




                                                            13
???? In REAL     Project in the world



 ? ??? ???? ??




                                    14
???? In REAL                          Project in the world



 ? ??? ???? ??
   ¨C ?? ??? ?? ??? ??(??,??? ???? ??)

   ¨C   ??? ?? ??? ?? ??? ??? ?? ?? ?? ??

   ¨C   ???? ?? ?? ??? ?? ??

   ¨C   Copy & Paste ? ?? ?? ??

   ¨C   ??? ???? ??? ???? (??? ??)




                                                         15
Normal Dejavu                                   Project in the world


      ? (???) ????? ?? ???? ?? ??

      ? ?? ????? ?? ??, ??, ??

      ? ?? ??? ?? ??? ????, ?? ??? ???? ??

      ? ??? ???? ? ??? ??? ??? ?? (Iterative)

      ? ?? ??

      ? ??? ?? ??, ??? ?? ??? ???

      ? ?? ?? ?? ??? ?? ? ???? ??? ??

      ? ??? ??? ?? ?? ????, ?? ??? ???? ??? ??

      ? (???) ????? ?? ???? ?? ??

      ? and Looping¡­


                                                                   16
¡­.
     17
2. ???????




             18
???????                          ???????



 ? ????
  ¨C ??? ??? ?? ??? ??? ?? ????

  ¨C ???? ??,

  ¨C ???? ???

  ¨C ?????? ???? ??? ???? ?




                                       19
Example #1                                          ???????




                           void f() {
  void f() {                   ¡­.
      ¡­.                       computeScore();
      // Compute score     }
      score = a * b + c;
      score -= discount;   void computeScore() {
  }                            score = a * b + c;
                               score -= discount;
                           }




                                                          20
Example #2                                              ???????



  public String name;




             private String name;

             public String getName(){
                 return name;
             }

             public String setName (String newName) {
                 name = newName;
             }



                                                              21
Example #3                                                 ???????



 if (user == null)
     plan = Plan.basic();
 else
     plan = user.getPlan();
                                 plan = user.getPlan();

                              <<interface>>
                                  User

                              +getPlan()




                                NullUser

                              +getPlan()      return Plan.basic();




                                                                     22
? ????? ????                                    ???????



 ? ??? ?? ?? ??

 ? ??? ???? ?? ??? ??

 ? ??? ???? ??

 ? ? ? ??? ????? ? ? ???




                    ???? ????? ?? ??? ?? ? ? ????

                                                      23
????                                 ???????



? ????(Smells)

? ????(Catalogue)

? ???? ?? ???(Automated Unit Test)




                                           24
???                 ???????




      ? ??? ???

      ? ????? ??

      ? ???? ????




                          25
?? ????? ????                                         ???????



 ? The Rule of Three ¨C DRY(Don¡¯t Repeat Yourself)??

 ? ??? ??? ?

 ? ??? ?? ? ?

 ? ?? ??? ? ?




                                                            26
??? ??                                 ???????



 ? ?????? ? ???? ?? ??? ?? ??? ???, ??
  ?? ????? ? ??? ????? ?? ??? ? ? ??.




 ? ? ??? ??? ????? ?? ??
   ¨C ??? ?? ??? ?? ???? ???? ???? ?? ????.

   ¨C ??? ??? ????.



                                             27
??? ????? 3? ??                ???????



              ? ??? ??? ??? ???
                  ?? ????.

              ? ??? ???? ??? ??
                  ??.

              ? ???? ?? ??? ???
                  ? ??? ???.




                                     28
???? ??            ???????



 ? ????? ???? ??




                         29
3.??? ??




           30
?? ??? Automated Unit Test         ??? ??



 ? ¡°???? ??? ?? ??? ??? ??? ???? ??? ?
   ? ???.¡± ¨C ?? ???

 ? ?? ???? ???? ?? ??? ?????
   = JUnit




                                         31
??? ?? ?? Test-Driven Development   ??? ??



 ? ???? ?? ??? ???? ??? ?? ?? ??? ??

 ? ¡°????? ???? ?? ???? ??? ??? ?? ????.¡±

 ? ??? ??? ??? ??? ??? ?? ??




                                           32
TDD ??                                    ??? ??




         ?? Ask
          ???? ?????? ???? ????. (FAIL)


         ?? Respond
          ???? ???? ??? ???? ??? ????. (PASS)


         ?? Refine
          ????? ????, ???? ?? ????, ??? ??
          ??? ?? ??? ????. (REFACTORING)


         ?? Repeat
          ?? ??? ?? ??? ?? ????.

                                                33
4.????? ??
Refactoring and Design




                         34
????? ??                            ????? ??



 ? ?? ?? = ?? ?? + ?? ??

 ? ?? ?? = ?? ?? + ?? ?? + ??? ??
  + ?? ??




                                           35
?? ?? Upfront Design                     ????? ??



 ? ?? ??
   ¨C ? ?? ??? ??? ??? SW??? ?? ??? ??? ?? ??

   ¨C ???? ??? ??? ??? ??? ?? ????.

   ¨C ? ? ??? ??? ??? ????.

 ? ??? ??? ???? ??? ?? ???
   ¨C ??? ???? ???? ? ?? ??? ???? ????.

   ¨C ? ? ??? ??? ?? ??, ????? ?????? ? ?????.

   ¨C ??? ??? ??? ????? ?? ???? ?? ?????.

                                                36
????? ??                                 ????? ??



 ? ????? ?? ??? ????(upfront design)? ????
  ? ????? ????? ???? ????.
   ¨C ????? ?? ??? ???? ???. ??? ???? ?? ?? ?
    ?? ????? ??(reasonable)? ???? ???.

   ¨C ??? ???? ??? ???? ??? ?? ? ? ??? ??.

   ¨C ??? ???? ? ? ??? ??? ????? ??? ?? ?? ??
    ?, ? ??? ?? ??? ????.




                                                37
5.?? ?? ?? ??
     Smells




                38
?? Intuition                                         Smells

               1997? 5? 11?

                 ??? ???? ????? IBM? ??? ?? ??? ? ??(Deep
               blue)? ?? ?? ??? ¡®?? ?????(Kasparov)¡¯?? 6 ??
               ??? ??? ???? ???.


                ¡º? ??¡»? 2m? ? ??? ?? 1.4 ??? ??? ????
               ??? ??? ??? ??? ??? ?? ?? 6?? IBM? ??
               ????? ?? 8? ?? 32?? ???? ????? 512?? ??
               ?? ?? ???? ??? 2?? ?? ??? ?? ?? ?? ????.
               ¡º?¡»? ??? ¡º??? ???¡»? ??? ?? ??? ??? ???
               ???? 1?? 2??? ??(ÐÐñR)? ????? ??? ? ? ??
               ??? ???, ?? ? ? ??? ????? ?? ?? ??? ???
               ?? ???? ??. ??? ? ?? ?? ???? ??? ???? ??
               ?? ??? 2?? ????? ??? ?? ??? ?? ???? ??
               ?? ?? ???? ??? ??? ?????. ?? ? ?? ??
               ??? ???? ???? ??? ?? ??? ?? ???? ??? ?
               ?? ?? ????? ???? ?????? ??? ???? ??.
               ???¡º?¡»? ???? ??? ???? ¡º???? ??? ???
               ?? ???¡»?? ? ? ??


               ?? 5???? ? ? ????? ??? 1? 3? 1?? ???.


               - New York Times 1997.5

                                                              39
?? ?? ?? ?? #1                                          Smells

                                   ¡°?? ??? ???, ???!¡± ¨C ???

  ¨C ????(Duplicate code)
  ¨C ? ???(Long Method)
  ¨C ??? ???(Large class)
  ¨C ? ???? ???(Long Parameter List)
  ¨C ??? ??(Divergent Change)
  ¨C ??? ??(Shotgun Surgery)
  ¨C ?? ??(Feature Envy)
  ¨C ??? ???(Data Clumps)
  ¨C ???? ?? ??(Primitive Obsession)
  ¨C ??? ??(Switch Statements)
  ¨C ?? ?? ??(Parallel Inheritance Hierarchies)


                                                              40
?? ?? ?? ?? #2                                                        Smells

                    ¡°???? ??? ? ?? ??? ??? ??? ?
                        ??. ??? ??? ? ?? ??? ????¡±
  ¨C ??? ???(Lazy Class)                    ¨C ?? ???
  ¨C ??? ???(Speculative Generality)
  ¨C ?? ??(Temporary Field)
  ¨C ??? ??(Message Chains)
  ¨C ???(Middle Man)
  ¨C ???? ????(Inappropriate Intimacy)
  ¨C ?????? ?? ?? ???(Alternative Classes with Different Interfaces)
  ¨C ???? ????? ???(Incomplete Library Class)
  ¨C ??? ???(Data Class)
  ¨C ??? ??(Refused Bequest)
  ¨C ??(Comments)


                                                                           41
? ??? Long Method                       Smells



 ? ???? ??? ?? ????? ?? ?? ???.
   ¨C ???? ??? ?? ??? ???? ? ??? ??.

   ¨C ??, ?? ???? ????

      ????? ??(Semantic Distance)? ??

 ? ??? 99%? ???? ??
   ¨C Extract Method

 ? ???? ????
   ¨C Decompose Conditional

                                             42
? ??? Long Method                    Smells



 ? Decompose Conditional? ??? ?? ?




                                          43
?? ?? Naming                                             Smells



 ? addCourse( course );

 ? getNodesArrayList();

 ? makeIt();

 ? getData();

 ? sort();

 ? processItem();

                          ??? ?? ???? ??? ? ?? ??? ??.
                          W44()


                                                              44
?? ?? Naming                                                                         Smells


 ? ???? ???? ?? ??? ???? ? ??.
  maskClear2( plMoney )

 ???? ???? ??? ??? ???? ???? ??
 System.out.println(LMoney.toCommaFormat(120000));
 System.out.println(LMoney.toCommaFormat(120000, true)); // () ?? ??? ???? ??? ??
 System.out.println(LMoney.toCommaFormat(070712093801.9283));

 ? ??(argument)? ??? ?? ?? ?? ??? ??? ?? ??
  Money(String a) {
      Money(String a, false); }

 ? ???? ?? ?? ??
    /**
     * basic ?? ????? ?? ?? Array
     */
    private static final String basicA[] = { "", "?", "?", "?" };

    /**
     * base ?? ????? ?? ?? Array
     */
    private static final String baseA[] = { "", "?", "?", "?", "?" };


                                                                                          45
?? Comments                         Smells



 ? ?? ??? ???? ?? ??? ??????
   ¨C Extract Method

 ? ??? ????? ??? ??? ??? ??????
   ¨C Rename Method

 ? ???? ??? ???? ?? ??? ???? ?? ??? ?
   ?? ??? ?????
   ¨C Introduce Assertion




                                         46
?? Comments                                  Smells



 ? ??? ????
   ¨C ??? ?? ???. ? Sweet Smells

   ¨C But, ??(what)? ??? ????? ???? ???.

   ¨C ??(why)? ???? ??? ???? ??.

   ¨C ??? ??? ?? ??? ???? ???? ???? ??? ??.

 ? ?? ??? ???? ?? ???? ??????
   ¨C Extract Method




                                                  47
?? Comments                                         Smells


 ? ?? ???
   ¨C ??? ?? ?? ??? ???? ?? (???)

   ¨C ??? ?? : ????? ??? ??? ?? ??? ???? ??
     (???, ??? ??? ??)

   ¨C ???? : Todo, Release ?? ? ?? ?? ??? ???? ??
     (???, ??? ?? ??? ??)

   ¨C ??? ?? : ? ?? ?? ??? ?? ?? ???? ??.
     ??? ??? ??? ? ??? ??? ?(??)

   ¨C ??? ?? ?? (??)

   ¨C ?????? ??? ? ?? ??: ????? ????, ??? ?? ?? ??(??)
     - from code complete


                                                         48
?? ¨C Example #1                                                               Smells


  /**
   *
   * laf.xml ??? ??? ?? ???? ??? ????.
   *
   *    1) test-mode
   *      ???? ??? ?? ???? ? ?? true? ????, ? ?? test-receivers ??? ????.
   *      ????? test-mode? false? ????.
   *
   *    2) spec
   *      spec ? ?? ??? ??? mailer server??? ??? ?? ?? ????.
   *      spec ? name? ???? ?? ?? default ?? ????,
   *      default ? ??? ? ? ??? ???? ???? ?? ???? spec ? name? example ? ?? ????.
   *      ??? ? ??, default?? ??? ???? ?? ??? ?? overwrite ??.
   *      ?, ???? ??? default? ?? ????? ? ??? spec? name? ???? ??? ? ?? ??? ??.
   *      spec ? LMail ??? ???? ????, ??? ?? ?? default spec? ????.
   *
   *    3) body-template
   *      html ??? ??? ???? ? ?? ????.
   *      html string ? ?? ???? send? ?? ???, ???? html ?? ??? ????? ? ??? ????.
   *      ?? ?? template html ??? ????? ????? html??? ????.
   *      mail-template ? spec? ????? LMail ??? ??? ????.
   *      ??? ?? ?? default mail-template? ????, sub-directory??? ????.
   *

                                                                                    49
?? ¨C Example #2                                              Smells


    public static void writeMail(String[] args) {

       // ??? ??? ?? ???? LDATA? ???? ??
       LData ld = new LData("userInfo");

       // ???? ??? ????.
       // ? ???? userName ??? ???? ???? ?
       ld.setString("userName", "Jubong, Park");

       // ??? ??? ???. ??? ??? LData?? name ?? ????.
       LMultiData lm = new LMultiData("friends");
       lm.add("name", "Hyori, Kim");
       lm.add("name", "Yeesul, Lee");
       lm.add("name", "Dongwook, Park");

       // [ ] ? ???? ?????, ??? ??? ???? ???? ??? ????? ??
       StringBuffer imsi = new StringBuffer();
       for(int i=0; i<lm.keySize("name"); i++) {
           imsi.append("[");
           imsi.append(lm.getString("name",i));
           imsi.append("] ");
       }
       ld.setString("friends", imsi.toString());

                                                                  50
?? ¨C Example #2                                      Smells




     public static void writeMail(String[] args) {

            Mail mail = new Mail();

            mail.setSender("Jubong, Park");

            mail.addReceiver("Hyori, Kim");
            mail.addReceiver("Yeesul, Lee");
            mail.addReceiver("Dongwook, Park");

               ¡­
               ¡­



                                                          51
?? ¨C Example #3                                    Smells




  public static final int LowHandleLength = 400;

  // ???? ?? MaxTilt? ??????? ???? ????.
  public static final int MaxTilt = 200;




                                                        52
??? ?? ?? Feature Envy              Smells



 ? ??? ??? ?? ????? ?? ???? ?? ??? ??
   ???
   ¨C Move Method

 ? ?? ???? ???? ??? ???? ?? ???
   ¨C Extract Method ? Move Method

 ? ?? ???? ?? ??? ? ??? ???.




                                         53
? ???? ??? Long Parameter List               Smells



 ? ????? ??? ??? ??
    ¨C ????? ?? ??. ? ??? ??? ?? ????? ???.

 ? ???? ???? ?????
    ¨C ???? ???.

    ¨C ?????? ????? ???? ????.




                                                  54
? ???? ??? Long Parameter List         Smells



 ? OOP? ???

 ? ??(Object)? ?? ??? ???? ??? ? ??.
   ? ???? ???? ? ? ? ??!
    ¨C Replace Parameter with Method

    ¨C Preserve Whole Object

    ¨C Introduce Parameter Object




                                            55
??? ?? Divergent Change                           Smells



 ? ? ???? ?? ??? ?? ?? ???? ??
    ¨C ?????? ???? ??? ??. ??? ??? ??? ???? ??
      ? ? ?? ????? ???? ??. ???, ??? ??? ?? ? ?
      ?? ??? ?? ?? ???? ????

 ? ?? ??? ?? ???? ?? ???? ??? ??
    ¨C Extract Method




                                                       56
?? ?? Temporary Fields                    Smells



  ? ?? ??? ?? ?? ??? ?? ????? ? ? ??? ?
   ??? ??? ???.
    ¨C Replace Temp with Query

    ¨C Replace Parameter with Method

  ? ?? ?? ??? ??????
    ¨C Extract Class




                                               57
?? ?? Duplicate code                                     Smells



 ? ? ?? ??? ??? ?? = ?? ??!!
    ¨C ?? ??? ??? ???!

 ? ??? ??? ?? ??? ??
    ¨C ?? ??? ??? ? Extract Method

    ¨C ?? ???? ????? ? Extract Method and Pull Up Field

    ¨C ?? ???? ??? ????? Extract Class




                                                              58
??? ?? Switch statements                                            Smells



 ? ? ??? ???? ????? ??? ?? ??!
    ¨C ???(switch) ??? ????? ??!

 ? ??? ??? ? ? ?? ??? ???? ???? ?? ??
   ?? ??? ??? ??? ???? ??.
    ¨C Extract Method and Move Method
    ¨C Replace Type Code with Subclasses ?? Replace Type Code with
      State/Strategy
    ¨C Replace Conditional with Polymorphism
    ¨C Replace Parameter with Explicit Methods
    ¨C Introduce Null Object

                                                                         59
??? ?? Shotgun surgery                     Smells



 ? ??? ?? ?? ?? ?? ???? ??? ?? ??
    ¨C ???? ??? ??? ???? ?? ??? ??.

 ? ?????? ??/??? ???(one-to-one) ????? ?
   ??? ??.
    ¨C Move Method and Move Field

    ¨C Inline Class




                                                60
??? ??? Speculative Generality                 Smells



  ? ¡°??! ????? ??? ??? ??? ?! ?? ??? ???
   ?!¡±
    ¨C ???, ??? ? ? ???? ??? ???? ?? ??? ???.

  ? ??? ??? ???? ??! ??? ???? ??!
    ¨C ? ???? ?????? ? Collapse Hierarchy

    ¨C ???? ?? ??? ?????? ? Inline Class

    ¨C ?? ? ?? ??????? ? Remove Parameter




                                                    61
???? ?? ?? Primitive Obsession                      Smells



 ? ?? ?? ???????? ???? ???? ?? ?? ??
   ?? ??.

 ? ?????? ??? ????!
    ¨C ??, ??, ????, ????, ????, ??, ?? ?? ?? ?? ?
      ?? ? ? ??.

    ¨C Replace Data Value with Object

    ¨C Replace Array with Object




                                                         62
3. ???? ????
1.   Extract Method                   13. Replace Type Code with Subclasses
2.   Rename Method                    14. Replace Type Code with
                                          State/Strategy
3.   Introduce Assertion
                                      15. Replace Conditional with
4.   Move Method
                                          Polymorphism
5.   Decompose Conditional
                                      16. Introduce Null Object
6.   Move Field
                                      17. Preserve Whole Object
7.   Extract Class
                                      18. Collapse Hierarchy
8.   Inline Class
                                      19. Remove Parameter
9.   Replace Temp with Query
                                      20. Introduce Parameter Object
10. Pull Up Field ? Pull Down Field
                                      21. Replace Data Value with Object
11. Replace Parameter with Method
                                      22. Replace Array with Object
12. Replace Type Code with Class

                                                                                63
                                                                              ????
???? ???? #1                                                ???? ????


  ¨C Extract Method                  http://www.refactoring.com/catalog/
  ¨C Decompose Conditional
  ¨C Rename Method
  ¨C Introduce Assertion
  ¨C Move Method
  ¨C Move Field
  ¨C Extract Class
  ¨C Inline Class
  ¨C Replace Temp with Query
  ¨C Pull Up Field
  ¨C Replace Parameter with Method


                                                                          64
???? ???? #2                                ???? ????


  ¨C Replace Type Code with Class
  ¨C Replace Type Code with Subclasses
  ¨C Replace Type Code with State/Strategy
  ¨C Replace Conditional with Polymorphism
  ¨C Introduce Null Object
  ¨C Preserve Whole Object
  ¨C Collapse Hierarchy
  ¨C Remove Parameter
  ¨C Introduce Parameter Object
  ¨C Replace Data Value with Object
  ¨C Replace Array with Object


                                                    65
Extract Method                                                  ???? ????


   ?? ??? ???? ??? ?? ?




  - ???? ???, ??? ??? ? ????, ???, ???? ????.
  - ??? ? ??? ??, ??? ??? ??? ¡°???? ??(Semantic distance)¡±??.

                                                                        66
Rename Method                                              ???? ????


   ????? ??? ?? ??? ? ?? ?




              Customer                          Customer

       getinvcdtlmt()                  getInvoiceableCreditLimit()




  - ???? ?? ??? ? ??? ?? ??? ??? ??.
  - ?? ???? ??? ??? ????? ?? ????, ???? ????? ?? ???.
  - ???? ??? ?????? ?? ???? ???? ??? ? ?? ?? ???? ????!


                                                                     67
Introduce Assertion                                                   ???? ????


   ??? ??? ????? ?? ??? ???? ?? ?

      double getExpenseLimit() {
         // should have either expense limit or a primary project
         return (_expenseLimit != NULL_EXPENSE) ?
         _expenseLimit:
         _primaryProject.getMemberExpenseLimit();
      }



      double getExpenseLimit() {
         Assert.isTrue (_expenseLimit != NULL_EXPENSE || _primaryProject != null);
         return (_expenseLimit != NULL_EXPENSE) ?
         _expenseLimit:
         _primaryProject.getMemberExpenseLimit();
      }

   - ????? ???? ??? ????? ?? ?? ??.



                                                                                     68
Move Method                                           ???? ????


  ???? ??? ??? ????? ?? ???? ??? ? ?? ??? ?




  - ???? ?? ?? ??? ??? ???, ?? ???? ???? ??? ??? ?? ??? ??? ???? ?
    ? ? ??? ??? ???.
  - ?? ???? ??? ??? ?? ??? ??? ?? ????? ?? ????.


                                                                     69
Decompose Conditional                                                 ???? ????


   if-then-else?? ???? ???? ??? ?


      if ( date.before (SUMMER_START) || date.after(SUMMER_END) )
            charge = quantity * _winterRate + _winterServiceCharge;
      else charge = quantity * _summerRate;




      if ( notSummer(date) )
            charge = winterCharge(quantity);
      else charge = summerCharge (quantity);




                                                                              70
Move Field                            ???? ????


   ??? ??? ??? ????? ?? ???? ?? ? ?? ?? ? ?




                                              71
Extract Class                                            ???? ????


   ? ?? ???? ?? ? ?? ??? ???? ?? ?? ??




   - ????? ???? ??? ???? ??? ??, ??? ?? ?? ???? ?????.
   - ?? ??, ?? ? ???, ???!



                                                                 72
Inline Class                                             ???? ????


    ???? ?? ?? ?? ?? ?




   - ?? ???? ? ? ???? ??? ? ?? ????, ?? ?? ???? ?? ?? ?? ??? ? ?? ??.




                                                                        73
Replace Temp with Query                                              ???? ????


    ?? ??? ???? ???? ??? ????? ???? ?? ??


 double basePrice = _quantity * _itemPrice;
 if (basePrice > 1000)
     return basePrice * 0.95;
 else
     return basePrice * 0.98;                 if (basePrice() > 1000)
                                                  return basePrice() * 0.95;
                                              else
                                                  return basePrice() * 0.98;
                                              ...
                                              double basePrice() {
                                                  return _quantity * _itemPrice;
                                              }


   - ?? ??? ??? ????, ?? ????? ??? ???? ??? ??.
   - ????? ???? ???? ?? ??? ????.



                                                                                   74
Pull Up Field ? Pull Down Field   ???? ????


   ? ?? ?? ???? ?? ??? ?? ?? ??




   - ??? ????.




                                          75
Replace Parameter with Method                                          ???? ????


   ???? ?? ??? ?? ???? ????? ?? ?? ??


     int basePrice = _quantity * _itemPrice;
     discountLevel = getDiscountLevel();
     double finalPrice = discountedPrice (basePrice, discountLevel);




     int basePrice = _quantity * _itemPrice;
     double finalPrice = discountedPrice (basePrice);



  - ????? ???? ?????? ?? ? ? ?? ????? ?? ??? ??? ?? ? ??? ??.




                                                                               76
Replace Type Code with Class                          ???? ????


   ?? ???? ??? ??? ??? ?? ?? ?? ????? ??? ??




  - ?? ??? ???? ??? ??? ??? ??? ???? ??.
  - ?? ??? ???? ??? ?? ??? ??? ????? ??? ??? ? ? ??? ???.



                                                              77
Replace Type Code with Subclasses                     ???? ????


   ???? ??? ??? ?? ?? ????(immutable) ????? ?? ??




  - ?? ?? ??? ???? ??? ??? ? ??, ??? ??? ???? ???? ???.
  - ??? ??? ?? ??? ????? ???? ?? ???? ???? ??? ?? ????.



                                                              78
Replace Type Code with State/Strategy          ???? ????


   ??? ??? ?? ?? ??? ??? ?? ?? ??? ?????? ?? ?
   ?? ??




   - ??? ???? ?? ?? ??? ?? ??? ???? ?? ? ??.
   - ????? ?? ?? ?? ??? ?? ? ??.



                                                       79
Replace Conditional with
                                                                       ???? ????
Polymorphism #1
   ??? ??? ?? ?? ??? ???? ???? ??? ?? ??

      double getSpeed() {
         switch (_type) {
            case EUROPEAN:
               return getBaseSpeed();
            case AFRICAN:
               return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts;
            case NORWEIGIAN_BLUE:
               return (_isNailed) ? 0 : getBaseSpeed(_voltage);
         }
         throw new RuntimeException ("Should be unreachable");
      }



   - ???? ? ??? ?????? ?? ????? ???? ??? ?? ???? abstract? ???.




                                                                               80
Replace Conditional with
                                     ???? ????
Polymorphism #2
   ??? ??? ?? ?? ??? ???? ???? ??? ?? ??




                                             81
Introduce Null Object                                  ???? ????


    ???? ?(null) ??? ????? ?? ??




       if (customer == null)
          plan = BillingPlan.basic();
       else
          plan = customer.getPlan();




   - null ?? null ??? ????.
   - ???? ???, ??? ??? ?? ? ?? ??? ?? ?? ??? ???? ??? ?? ? ??? ????
     ???. ??? ? ??? ??? ??? ??? ????.


                                                                      82
Preserve Whole Object                                     ???? ????


   ?? ???? ?? ?? ?? ?? ?? ??? ?? ? ????? ?? ??


        int low = daysTempRange().getLow();
        int high = daysTempRange().getHigh();
        withinPlan = plan.withinRange(low, high);




        withinPlan = plan.withinRange(daysTempRange());



  - ???? ??? ?? ?? ???? ??, ?? ????? ? ??? ?? ??? ????. ?? ???? ???
    ??? ???? ?? ???? ???? ??? ???? ??.




                                                                      83
Collapse Hierarchy                ???? ????


   ?? ???? ?? ???? ?? ??? ?? ??




   - ????? ???? ?? ??? ?? ????.




                                          84
Remove Parameter                                  ???? ????


   ????? ? ?? ??? ??? ??? ?? ??




  - ?? ?? ????? ?? ??? ?? ???? ????.
  - ?, ???? ???? ???? ??, ???? ??? ? ? ???? ??.



                                                          85
Introduce Parameter Object                             ???? ????


   ?? ?? ??? ?????? ??? ?? ??




   - ???? ??? ?? ???? ????.
   - ??, ??? ??? ?? ??? ??? ??? ?? ?????? ????? ???? ??.



                                                               86
Replace Data Value with Object                            ???? ????


   ???? ???? ??? ??? ??? ??? ?? ??




  - ?? ??, ????? ?? ???? ???(String)?? ?? ? ? ???, ???? ???? ?? ????, ?
    ? ??? ???? ?? ?? ??? ??? ????? ??? ????.




                                                                          87
Replace Array with Object                         ???? ????


   ??? ?? ??? ?? ?? ??? ?? ??


           String[] row = new String[3];
           row [0] = "Liverpool";
           row [1] = "15";




           Performance row = new Performance();
           row.setName("Liverpool");
           row.setWins("15");




                                                          88

More Related Content

What's hot (17)

ODP
?? ???
Sungchul Park
?
PDF
TDD with JUnit 2
Junyoung Lee
?
PDF
Devon 2011-b-5 ???? ??? ?? ???
Daum DNA
?
PDF
??? TDD 101
?? ?
?
PDF
Legacy code refactoring video rental system
Jaehoon Oh
?
PDF
??? ??? ????? ??? (??: ??? ?)
SangIn Choung
?
PPTX
??? ?? ?? By googletest 1? ?? ??? ???? money ??
Mickey SJ Lee
?
PPTX
TDD
Henry Lee
?
PDF
Tdd live spring camp 2013
beom kyun choi
?
PPTX
C++ ????? ?? ??? ????
Heo Seungwook
?
PPTX
TEST?
beom kyun choi
?
PPTX
C++? TDD
?? ?
?
PPTX
IoT ???? ?? Embedded C?? TDD? ???
Taeyeop Kim
?
PDF
???? ??? ????? ?? ?? ??
SangIn Choung
?
PDF
Java the good parts
Sungchul Park
?
PPTX
??????? TDD
Sunghyouk Bae
?
PDF
Java performance and trouble shooting
Anna Choi
?
TDD with JUnit 2
Junyoung Lee
?
Devon 2011-b-5 ???? ??? ?? ???
Daum DNA
?
??? TDD 101
?? ?
?
Legacy code refactoring video rental system
Jaehoon Oh
?
??? ??? ????? ??? (??: ??? ?)
SangIn Choung
?
??? ?? ?? By googletest 1? ?? ??? ???? money ??
Mickey SJ Lee
?
Tdd live spring camp 2013
beom kyun choi
?
C++ ????? ?? ??? ????
Heo Seungwook
?
C++? TDD
?? ?
?
IoT ???? ?? Embedded C?? TDD? ???
Taeyeop Kim
?
???? ??? ????? ?? ?? ??
SangIn Choung
?
Java the good parts
Sungchul Park
?
??????? TDD
Sunghyouk Bae
?
Java performance and trouble shooting
Anna Choi
?

Viewers also liked (8)

PDF
Refactoring#9
Seungmin Yu
?
PPTX
Big refactoring #12
Jay Kim
?
PDF
[NEXT] Nextgram Refactoring
YoungSu Son
?
PPTX
Refactoring tutorial 1??[refactoring ??]
bbongcsu
?
PPTX
Refactoring tutorial
Bingu Shim
?
PPTX
Refactoring to Design Patterns
Eric De Carufel
?
PPTX
Refactoring vers les design patterns pyxis v2
Eric De Carufel
?
Refactoring#9
Seungmin Yu
?
Big refactoring #12
Jay Kim
?
[NEXT] Nextgram Refactoring
YoungSu Son
?
Refactoring tutorial 1??[refactoring ??]
bbongcsu
?
Refactoring tutorial
Bingu Shim
?
Refactoring to Design Patterns
Eric De Carufel
?
Refactoring vers les design patterns pyxis v2
Eric De Carufel
?
Ad

Similar to TDD&Refactoring Day 01: Refactoring (20)

PPTX
Refactoring Tutorial 1??[ Refactoring ??]
Bingu Shim
?
PPTX
Refactoring Tutorial 1??[ Refactoring ??]
Bingu Shim
?
PDF
???? ??? ?? ??
Hyosang Hong
?
PDF
Java ???? ??? ?? ??
Hyosang Hong
?
PDF
Refactoring
Suwon Chae
?
PDF
Ddd ch12-13
Kyungryul KIM
?
PPTX
[NDC12] ??? ??? ???? ? ??? ???? ????? ????? ??? - ???
ChangKyu Song
?
PPTX
????? ??? Agile skill set
?? ?
?
PPTX
??? - ??? ???? ?? ??
?? ?
?
PDF
???? 17?
?? ?
?
PPTX
[2012 03 17]clean_code 14? ?????
Jong Pil Won
?
PPTX
Domain Driven Design
hyun soomyung
?
PDF
????? ?? ??(Sig) ?? ??
Hong Hyo Sang
?
PDF
?????????? ????? ?????: ?? ?? ?? ??
Jay Park
?
PPTX
Code complete chapter 19, 20 organize
hanstar17
?
PPTX
???????? ???? (?? ??, Clean Code)
Jay Park
?
PDF
NDC2019 - ????? ?????? ??
Hoyoung Choi
?
PDF
Clean code ???????_2012_spring
??? Jintae Kim
?
PDF
C++ ?? ?? ?? ??
?? ?
?
PPTX
Clean code
bbongcsu
?
Refactoring Tutorial 1??[ Refactoring ??]
Bingu Shim
?
Refactoring Tutorial 1??[ Refactoring ??]
Bingu Shim
?
???? ??? ?? ??
Hyosang Hong
?
Java ???? ??? ?? ??
Hyosang Hong
?
Refactoring
Suwon Chae
?
Ddd ch12-13
Kyungryul KIM
?
[NDC12] ??? ??? ???? ? ??? ???? ????? ????? ??? - ???
ChangKyu Song
?
????? ??? Agile skill set
?? ?
?
??? - ??? ???? ?? ??
?? ?
?
???? 17?
?? ?
?
[2012 03 17]clean_code 14? ?????
Jong Pil Won
?
Domain Driven Design
hyun soomyung
?
????? ?? ??(Sig) ?? ??
Hong Hyo Sang
?
?????????? ????? ?????: ?? ?? ?? ??
Jay Park
?
Code complete chapter 19, 20 organize
hanstar17
?
???????? ???? (?? ??, Clean Code)
Jay Park
?
NDC2019 - ????? ?????? ??
Hoyoung Choi
?
Clean code ???????_2012_spring
??? Jintae Kim
?
C++ ?? ?? ?? ??
?? ?
?
Clean code
bbongcsu
?
Ad

TDD&Refactoring Day 01: Refactoring

  • 1. Day01 ? ?? ?? ??? ?? ???? 1
  • 2. ???? ??? ?????? ??? ????? ?? ???? ??? ? ??. ??? ??? ???? ??? ??? ??? ???? ??? ??? ? ?? ? ??. ??? ???? ??? ??? ??? ???? ?? ??? ??? ? ? ?. 2 ?????
  • 3. 1. ???? 1. ???? ?? 2. ?? ?? [??] 3 ?????
  • 5. ??? Abstraction ???? ?? ??? ??? ??? ??? ???? ?? ?? ???? ???? ???? ???? ?? ??? ?? 5
  • 6. ?? Inheritance ???? ?? ?? ???? ?? ?? ?? ???? ???? ???? ???? ??? ???? ? ? ???? ?? 6
  • 7. ??? Capsulation ???? ?? ??? ??? ?? ???? ???, ?? ??? ???? ?? ??? ?? ?? 7
  • 8. ??? Polymorphism ???? ?? polymorphism ??? ???? ?? ?? ? ¶àÐÎÐÔ ?? ?? ? ?? ?. ?? ? ?? ?? ??? ??? ?? ?? TYPE 8
  • 9. 2. ?? ?? [??] 9
  • 10. 2. ??????? 1. ???? ? ? ?? 2. ??????? 3. ??? ?? 4. ????? ?? 5. ?? ?? ?? ?? 10 ????
  • 11. 1. ???? ? ? ?? Project in the world 11
  • 12. ???? In IDEAL Project in the world ? ???? ???? ?? ??? ?? ?? ? ???? ??? ?? ? ???? ??? ???? ??? ??? ? ??? ??? ???? ? ?? ??? ??? ? ??? ??? ???? ?? ?? ?? ?? 12
  • 13. ???? In REAL Project in the world ? ???? ???? ?? ¨C ???? ?? ??? ??? Skill? ??? ?? ??. ¨C ??? ??? ?? ????. ¨C ?? ??? ???? ???? ?? ??? ???, ¨C ??? ??? ??? ??? ??. ¨C ???? ???? ?? ???? ???? ¨C ?? ??? ??? ???. ????? ? ???. 13
  • 14. ???? In REAL Project in the world ? ??? ???? ?? 14
  • 15. ???? In REAL Project in the world ? ??? ???? ?? ¨C ?? ??? ?? ??? ??(??,??? ???? ??) ¨C ??? ?? ??? ?? ??? ??? ?? ?? ?? ?? ¨C ???? ?? ?? ??? ?? ?? ¨C Copy & Paste ? ?? ?? ?? ¨C ??? ???? ??? ???? (??? ??) 15
  • 16. Normal Dejavu Project in the world ? (???) ????? ?? ???? ?? ?? ? ?? ????? ?? ??, ??, ?? ? ?? ??? ?? ??? ????, ?? ??? ???? ?? ? ??? ???? ? ??? ??? ??? ?? (Iterative) ? ?? ?? ? ??? ?? ??, ??? ?? ??? ??? ? ?? ?? ?? ??? ?? ? ???? ??? ?? ? ??? ??? ?? ?? ????, ?? ??? ???? ??? ?? ? (???) ????? ?? ???? ?? ?? ? and Looping¡­ 16
  • 17. ¡­. 17
  • 19. ??????? ??????? ? ???? ¨C ??? ??? ?? ??? ??? ?? ???? ¨C ???? ??, ¨C ???? ??? ¨C ?????? ???? ??? ???? ? 19
  • 20. Example #1 ??????? void f() { void f() { ¡­. ¡­. computeScore(); // Compute score } score = a * b + c; score -= discount; void computeScore() { } score = a * b + c; score -= discount; } 20
  • 21. Example #2 ??????? public String name; private String name; public String getName(){ return name; } public String setName (String newName) { name = newName; } 21
  • 22. Example #3 ??????? if (user == null) plan = Plan.basic(); else plan = user.getPlan(); plan = user.getPlan(); <<interface>> User +getPlan() NullUser +getPlan() return Plan.basic(); 22
  • 23. ? ????? ???? ??????? ? ??? ?? ?? ?? ? ??? ???? ?? ??? ?? ? ??? ???? ?? ? ? ? ??? ????? ? ? ??? ???? ????? ?? ??? ?? ? ? ???? 23
  • 24. ???? ??????? ? ????(Smells) ? ????(Catalogue) ? ???? ?? ???(Automated Unit Test) 24
  • 25. ??? ??????? ? ??? ??? ? ????? ?? ? ???? ???? 25
  • 26. ?? ????? ???? ??????? ? The Rule of Three ¨C DRY(Don¡¯t Repeat Yourself)?? ? ??? ??? ? ? ??? ?? ? ? ? ?? ??? ? ? 26
  • 27. ??? ?? ??????? ? ?????? ? ???? ?? ??? ?? ??? ???, ?? ?? ????? ? ??? ????? ?? ??? ? ? ??. ? ? ??? ??? ????? ?? ?? ¨C ??? ?? ??? ?? ???? ???? ???? ?? ????. ¨C ??? ??? ????. 27
  • 28. ??? ????? 3? ?? ??????? ? ??? ??? ??? ??? ?? ????. ? ??? ???? ??? ?? ??. ? ???? ?? ??? ??? ? ??? ???. 28
  • 29. ???? ?? ??????? ? ????? ???? ?? 29
  • 30. 3.??? ?? 30
  • 31. ?? ??? Automated Unit Test ??? ?? ? ¡°???? ??? ?? ??? ??? ??? ???? ??? ? ? ???.¡± ¨C ?? ??? ? ?? ???? ???? ?? ??? ????? = JUnit 31
  • 32. ??? ?? ?? Test-Driven Development ??? ?? ? ???? ?? ??? ???? ??? ?? ?? ??? ?? ? ¡°????? ???? ?? ???? ??? ??? ?? ????.¡± ? ??? ??? ??? ??? ??? ?? ?? 32
  • 33. TDD ?? ??? ?? ?? Ask ???? ?????? ???? ????. (FAIL) ?? Respond ???? ???? ??? ???? ??? ????. (PASS) ?? Refine ????? ????, ???? ?? ????, ??? ?? ??? ?? ??? ????. (REFACTORING) ?? Repeat ?? ??? ?? ??? ?? ????. 33
  • 35. ????? ?? ????? ?? ? ?? ?? = ?? ?? + ?? ?? ? ?? ?? = ?? ?? + ?? ?? + ??? ?? + ?? ?? 35
  • 36. ?? ?? Upfront Design ????? ?? ? ?? ?? ¨C ? ?? ??? ??? ??? SW??? ?? ??? ??? ?? ?? ¨C ???? ??? ??? ??? ??? ?? ????. ¨C ? ? ??? ??? ??? ????. ? ??? ??? ???? ??? ?? ??? ¨C ??? ???? ???? ? ?? ??? ???? ????. ¨C ? ? ??? ??? ?? ??, ????? ?????? ? ?????. ¨C ??? ??? ??? ????? ?? ???? ?? ?????. 36
  • 37. ????? ?? ????? ?? ? ????? ?? ??? ????(upfront design)? ???? ? ????? ????? ???? ????. ¨C ????? ?? ??? ???? ???. ??? ???? ?? ?? ? ?? ????? ??(reasonable)? ???? ???. ¨C ??? ???? ??? ???? ??? ?? ? ? ??? ??. ¨C ??? ???? ? ? ??? ??? ????? ??? ?? ?? ?? ?, ? ??? ?? ??? ????. 37
  • 38. 5.?? ?? ?? ?? Smells 38
  • 39. ?? Intuition Smells 1997? 5? 11? ??? ???? ????? IBM? ??? ?? ??? ? ??(Deep blue)? ?? ?? ??? ¡®?? ?????(Kasparov)¡¯?? 6 ?? ??? ??? ???? ???. ¡º? ??¡»? 2m? ? ??? ?? 1.4 ??? ??? ???? ??? ??? ??? ??? ??? ?? ?? 6?? IBM? ?? ????? ?? 8? ?? 32?? ???? ????? 512?? ?? ?? ?? ???? ??? 2?? ?? ??? ?? ?? ?? ????. ¡º?¡»? ??? ¡º??? ???¡»? ??? ?? ??? ??? ??? ???? 1?? 2??? ??(ÐÐñR)? ????? ??? ? ? ?? ??? ???, ?? ? ? ??? ????? ?? ?? ??? ??? ?? ???? ??. ??? ? ?? ?? ???? ??? ???? ?? ?? ??? 2?? ????? ??? ?? ??? ?? ???? ?? ?? ?? ???? ??? ??? ?????. ?? ? ?? ?? ??? ???? ???? ??? ?? ??? ?? ???? ??? ? ?? ?? ????? ???? ?????? ??? ???? ??. ???¡º?¡»? ???? ??? ???? ¡º???? ??? ??? ?? ???¡»?? ? ? ?? ?? 5???? ? ? ????? ??? 1? 3? 1?? ???. - New York Times 1997.5 39
  • 40. ?? ?? ?? ?? #1 Smells ¡°?? ??? ???, ???!¡± ¨C ??? ¨C ????(Duplicate code) ¨C ? ???(Long Method) ¨C ??? ???(Large class) ¨C ? ???? ???(Long Parameter List) ¨C ??? ??(Divergent Change) ¨C ??? ??(Shotgun Surgery) ¨C ?? ??(Feature Envy) ¨C ??? ???(Data Clumps) ¨C ???? ?? ??(Primitive Obsession) ¨C ??? ??(Switch Statements) ¨C ?? ?? ??(Parallel Inheritance Hierarchies) 40
  • 41. ?? ?? ?? ?? #2 Smells ¡°???? ??? ? ?? ??? ??? ??? ? ??. ??? ??? ? ?? ??? ????¡± ¨C ??? ???(Lazy Class) ¨C ?? ??? ¨C ??? ???(Speculative Generality) ¨C ?? ??(Temporary Field) ¨C ??? ??(Message Chains) ¨C ???(Middle Man) ¨C ???? ????(Inappropriate Intimacy) ¨C ?????? ?? ?? ???(Alternative Classes with Different Interfaces) ¨C ???? ????? ???(Incomplete Library Class) ¨C ??? ???(Data Class) ¨C ??? ??(Refused Bequest) ¨C ??(Comments) 41
  • 42. ? ??? Long Method Smells ? ???? ??? ?? ????? ?? ?? ???. ¨C ???? ??? ?? ??? ???? ? ??? ??. ¨C ??, ?? ???? ???? ????? ??(Semantic Distance)? ?? ? ??? 99%? ???? ?? ¨C Extract Method ? ???? ???? ¨C Decompose Conditional 42
  • 43. ? ??? Long Method Smells ? Decompose Conditional? ??? ?? ? 43
  • 44. ?? ?? Naming Smells ? addCourse( course ); ? getNodesArrayList(); ? makeIt(); ? getData(); ? sort(); ? processItem(); ??? ?? ???? ??? ? ?? ??? ??. W44() 44
  • 45. ?? ?? Naming Smells ? ???? ???? ?? ??? ???? ? ??. maskClear2( plMoney ) ???? ???? ??? ??? ???? ???? ?? System.out.println(LMoney.toCommaFormat(120000)); System.out.println(LMoney.toCommaFormat(120000, true)); // () ?? ??? ???? ??? ?? System.out.println(LMoney.toCommaFormat(070712093801.9283)); ? ??(argument)? ??? ?? ?? ?? ??? ??? ?? ?? Money(String a) { Money(String a, false); } ? ???? ?? ?? ?? /** * basic ?? ????? ?? ?? Array */ private static final String basicA[] = { "", "?", "?", "?" }; /** * base ?? ????? ?? ?? Array */ private static final String baseA[] = { "", "?", "?", "?", "?" }; 45
  • 46. ?? Comments Smells ? ?? ??? ???? ?? ??? ?????? ¨C Extract Method ? ??? ????? ??? ??? ??? ?????? ¨C Rename Method ? ???? ??? ???? ?? ??? ???? ?? ??? ? ?? ??? ????? ¨C Introduce Assertion 46
  • 47. ?? Comments Smells ? ??? ???? ¨C ??? ?? ???. ? Sweet Smells ¨C But, ??(what)? ??? ????? ???? ???. ¨C ??(why)? ???? ??? ???? ??. ¨C ??? ??? ?? ??? ???? ???? ???? ??? ??. ? ?? ??? ???? ?? ???? ?????? ¨C Extract Method 47
  • 48. ?? Comments Smells ? ?? ??? ¨C ??? ?? ?? ??? ???? ?? (???) ¨C ??? ?? : ????? ??? ??? ?? ??? ???? ?? (???, ??? ??? ??) ¨C ???? : Todo, Release ?? ? ?? ?? ??? ???? ?? (???, ??? ?? ??? ??) ¨C ??? ?? : ? ?? ?? ??? ?? ?? ???? ??. ??? ??? ??? ? ??? ??? ?(??) ¨C ??? ?? ?? (??) ¨C ?????? ??? ? ?? ??: ????? ????, ??? ?? ?? ??(??) - from code complete 48
  • 49. ?? ¨C Example #1 Smells /** * * laf.xml ??? ??? ?? ???? ??? ????. * * 1) test-mode * ???? ??? ?? ???? ? ?? true? ????, ? ?? test-receivers ??? ????. * ????? test-mode? false? ????. * * 2) spec * spec ? ?? ??? ??? mailer server??? ??? ?? ?? ????. * spec ? name? ???? ?? ?? default ?? ????, * default ? ??? ? ? ??? ???? ???? ?? ???? spec ? name? example ? ?? ????. * ??? ? ??, default?? ??? ???? ?? ??? ?? overwrite ??. * ?, ???? ??? default? ?? ????? ? ??? spec? name? ???? ??? ? ?? ??? ??. * spec ? LMail ??? ???? ????, ??? ?? ?? default spec? ????. * * 3) body-template * html ??? ??? ???? ? ?? ????. * html string ? ?? ???? send? ?? ???, ???? html ?? ??? ????? ? ??? ????. * ?? ?? template html ??? ????? ????? html??? ????. * mail-template ? spec? ????? LMail ??? ??? ????. * ??? ?? ?? default mail-template? ????, sub-directory??? ????. * 49
  • 50. ?? ¨C Example #2 Smells public static void writeMail(String[] args) { // ??? ??? ?? ???? LDATA? ???? ?? LData ld = new LData("userInfo"); // ???? ??? ????. // ? ???? userName ??? ???? ???? ? ld.setString("userName", "Jubong, Park"); // ??? ??? ???. ??? ??? LData?? name ?? ????. LMultiData lm = new LMultiData("friends"); lm.add("name", "Hyori, Kim"); lm.add("name", "Yeesul, Lee"); lm.add("name", "Dongwook, Park"); // [ ] ? ???? ?????, ??? ??? ???? ???? ??? ????? ?? StringBuffer imsi = new StringBuffer(); for(int i=0; i<lm.keySize("name"); i++) { imsi.append("["); imsi.append(lm.getString("name",i)); imsi.append("] "); } ld.setString("friends", imsi.toString()); 50
  • 51. ?? ¨C Example #2 Smells public static void writeMail(String[] args) { Mail mail = new Mail(); mail.setSender("Jubong, Park"); mail.addReceiver("Hyori, Kim"); mail.addReceiver("Yeesul, Lee"); mail.addReceiver("Dongwook, Park"); ¡­ ¡­ 51
  • 52. ?? ¨C Example #3 Smells public static final int LowHandleLength = 400; // ???? ?? MaxTilt? ??????? ???? ????. public static final int MaxTilt = 200; 52
  • 53. ??? ?? ?? Feature Envy Smells ? ??? ??? ?? ????? ?? ???? ?? ??? ?? ??? ¨C Move Method ? ?? ???? ???? ??? ???? ?? ??? ¨C Extract Method ? Move Method ? ?? ???? ?? ??? ? ??? ???. 53
  • 54. ? ???? ??? Long Parameter List Smells ? ????? ??? ??? ?? ¨C ????? ?? ??. ? ??? ??? ?? ????? ???. ? ???? ???? ????? ¨C ???? ???. ¨C ?????? ????? ???? ????. 54
  • 55. ? ???? ??? Long Parameter List Smells ? OOP? ??? ? ??(Object)? ?? ??? ???? ??? ? ??. ? ???? ???? ? ? ? ??! ¨C Replace Parameter with Method ¨C Preserve Whole Object ¨C Introduce Parameter Object 55
  • 56. ??? ?? Divergent Change Smells ? ? ???? ?? ??? ?? ?? ???? ?? ¨C ?????? ???? ??? ??. ??? ??? ??? ???? ?? ? ? ?? ????? ???? ??. ???, ??? ??? ?? ? ? ?? ??? ?? ?? ???? ???? ? ?? ??? ?? ???? ?? ???? ??? ?? ¨C Extract Method 56
  • 57. ?? ?? Temporary Fields Smells ? ?? ??? ?? ?? ??? ?? ????? ? ? ??? ? ??? ??? ???. ¨C Replace Temp with Query ¨C Replace Parameter with Method ? ?? ?? ??? ?????? ¨C Extract Class 57
  • 58. ?? ?? Duplicate code Smells ? ? ?? ??? ??? ?? = ?? ??!! ¨C ?? ??? ??? ???! ? ??? ??? ?? ??? ?? ¨C ?? ??? ??? ? Extract Method ¨C ?? ???? ????? ? Extract Method and Pull Up Field ¨C ?? ???? ??? ????? Extract Class 58
  • 59. ??? ?? Switch statements Smells ? ? ??? ???? ????? ??? ?? ??! ¨C ???(switch) ??? ????? ??! ? ??? ??? ? ? ?? ??? ???? ???? ?? ?? ?? ??? ??? ??? ???? ??. ¨C Extract Method and Move Method ¨C Replace Type Code with Subclasses ?? Replace Type Code with State/Strategy ¨C Replace Conditional with Polymorphism ¨C Replace Parameter with Explicit Methods ¨C Introduce Null Object 59
  • 60. ??? ?? Shotgun surgery Smells ? ??? ?? ?? ?? ?? ???? ??? ?? ?? ¨C ???? ??? ??? ???? ?? ??? ??. ? ?????? ??/??? ???(one-to-one) ????? ? ??? ??. ¨C Move Method and Move Field ¨C Inline Class 60
  • 61. ??? ??? Speculative Generality Smells ? ¡°??! ????? ??? ??? ??? ?! ?? ??? ??? ?!¡± ¨C ???, ??? ? ? ???? ??? ???? ?? ??? ???. ? ??? ??? ???? ??! ??? ???? ??! ¨C ? ???? ?????? ? Collapse Hierarchy ¨C ???? ?? ??? ?????? ? Inline Class ¨C ?? ? ?? ??????? ? Remove Parameter 61
  • 62. ???? ?? ?? Primitive Obsession Smells ? ?? ?? ???????? ???? ???? ?? ?? ?? ?? ??. ? ?????? ??? ????! ¨C ??, ??, ????, ????, ????, ??, ?? ?? ?? ?? ? ?? ? ? ??. ¨C Replace Data Value with Object ¨C Replace Array with Object 62
  • 63. 3. ???? ???? 1. Extract Method 13. Replace Type Code with Subclasses 2. Rename Method 14. Replace Type Code with State/Strategy 3. Introduce Assertion 15. Replace Conditional with 4. Move Method Polymorphism 5. Decompose Conditional 16. Introduce Null Object 6. Move Field 17. Preserve Whole Object 7. Extract Class 18. Collapse Hierarchy 8. Inline Class 19. Remove Parameter 9. Replace Temp with Query 20. Introduce Parameter Object 10. Pull Up Field ? Pull Down Field 21. Replace Data Value with Object 11. Replace Parameter with Method 22. Replace Array with Object 12. Replace Type Code with Class 63 ????
  • 64. ???? ???? #1 ???? ???? ¨C Extract Method http://www.refactoring.com/catalog/ ¨C Decompose Conditional ¨C Rename Method ¨C Introduce Assertion ¨C Move Method ¨C Move Field ¨C Extract Class ¨C Inline Class ¨C Replace Temp with Query ¨C Pull Up Field ¨C Replace Parameter with Method 64
  • 65. ???? ???? #2 ???? ???? ¨C Replace Type Code with Class ¨C Replace Type Code with Subclasses ¨C Replace Type Code with State/Strategy ¨C Replace Conditional with Polymorphism ¨C Introduce Null Object ¨C Preserve Whole Object ¨C Collapse Hierarchy ¨C Remove Parameter ¨C Introduce Parameter Object ¨C Replace Data Value with Object ¨C Replace Array with Object 65
  • 66. Extract Method ???? ???? ?? ??? ???? ??? ?? ? - ???? ???, ??? ??? ? ????, ???, ???? ????. - ??? ? ??? ??, ??? ??? ??? ¡°???? ??(Semantic distance)¡±??. 66
  • 67. Rename Method ???? ???? ????? ??? ?? ??? ? ?? ? Customer Customer getinvcdtlmt() getInvoiceableCreditLimit() - ???? ?? ??? ? ??? ?? ??? ??? ??. - ?? ???? ??? ??? ????? ?? ????, ???? ????? ?? ???. - ???? ??? ?????? ?? ???? ???? ??? ? ?? ?? ???? ????! 67
  • 68. Introduce Assertion ???? ???? ??? ??? ????? ?? ??? ???? ?? ? double getExpenseLimit() { // should have either expense limit or a primary project return (_expenseLimit != NULL_EXPENSE) ? _expenseLimit: _primaryProject.getMemberExpenseLimit(); } double getExpenseLimit() { Assert.isTrue (_expenseLimit != NULL_EXPENSE || _primaryProject != null); return (_expenseLimit != NULL_EXPENSE) ? _expenseLimit: _primaryProject.getMemberExpenseLimit(); } - ????? ???? ??? ????? ?? ?? ??. 68
  • 69. Move Method ???? ???? ???? ??? ??? ????? ?? ???? ??? ? ?? ??? ? - ???? ?? ?? ??? ??? ???, ?? ???? ???? ??? ??? ?? ??? ??? ???? ? ? ? ??? ??? ???. - ?? ???? ??? ??? ?? ??? ??? ?? ????? ?? ????. 69
  • 70. Decompose Conditional ???? ???? if-then-else?? ???? ???? ??? ? if ( date.before (SUMMER_START) || date.after(SUMMER_END) ) charge = quantity * _winterRate + _winterServiceCharge; else charge = quantity * _summerRate; if ( notSummer(date) ) charge = winterCharge(quantity); else charge = summerCharge (quantity); 70
  • 71. Move Field ???? ???? ??? ??? ??? ????? ?? ???? ?? ? ?? ?? ? ? 71
  • 72. Extract Class ???? ???? ? ?? ???? ?? ? ?? ??? ???? ?? ?? ?? - ????? ???? ??? ???? ??? ??, ??? ?? ?? ???? ?????. - ?? ??, ?? ? ???, ???! 72
  • 73. Inline Class ???? ???? ???? ?? ?? ?? ?? ? - ?? ???? ? ? ???? ??? ? ?? ????, ?? ?? ???? ?? ?? ?? ??? ? ?? ??. 73
  • 74. Replace Temp with Query ???? ???? ?? ??? ???? ???? ??? ????? ???? ?? ?? double basePrice = _quantity * _itemPrice; if (basePrice > 1000) return basePrice * 0.95; else return basePrice * 0.98; if (basePrice() > 1000) return basePrice() * 0.95; else return basePrice() * 0.98; ... double basePrice() { return _quantity * _itemPrice; } - ?? ??? ??? ????, ?? ????? ??? ???? ??? ??. - ????? ???? ???? ?? ??? ????. 74
  • 75. Pull Up Field ? Pull Down Field ???? ???? ? ?? ?? ???? ?? ??? ?? ?? ?? - ??? ????. 75
  • 76. Replace Parameter with Method ???? ???? ???? ?? ??? ?? ???? ????? ?? ?? ?? int basePrice = _quantity * _itemPrice; discountLevel = getDiscountLevel(); double finalPrice = discountedPrice (basePrice, discountLevel); int basePrice = _quantity * _itemPrice; double finalPrice = discountedPrice (basePrice); - ????? ???? ?????? ?? ? ? ?? ????? ?? ??? ??? ?? ? ??? ??. 76
  • 77. Replace Type Code with Class ???? ???? ?? ???? ??? ??? ??? ?? ?? ?? ????? ??? ?? - ?? ??? ???? ??? ??? ??? ??? ???? ??. - ?? ??? ???? ??? ?? ??? ??? ????? ??? ??? ? ? ??? ???. 77
  • 78. Replace Type Code with Subclasses ???? ???? ???? ??? ??? ?? ?? ????(immutable) ????? ?? ?? - ?? ?? ??? ???? ??? ??? ? ??, ??? ??? ???? ???? ???. - ??? ??? ?? ??? ????? ???? ?? ???? ???? ??? ?? ????. 78
  • 79. Replace Type Code with State/Strategy ???? ???? ??? ??? ?? ?? ??? ??? ?? ?? ??? ?????? ?? ? ?? ?? - ??? ???? ?? ?? ??? ?? ??? ???? ?? ? ??. - ????? ?? ?? ?? ??? ?? ? ??. 79
  • 80. Replace Conditional with ???? ???? Polymorphism #1 ??? ??? ?? ?? ??? ???? ???? ??? ?? ?? double getSpeed() { switch (_type) { case EUROPEAN: return getBaseSpeed(); case AFRICAN: return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts; case NORWEIGIAN_BLUE: return (_isNailed) ? 0 : getBaseSpeed(_voltage); } throw new RuntimeException ("Should be unreachable"); } - ???? ? ??? ?????? ?? ????? ???? ??? ?? ???? abstract? ???. 80
  • 81. Replace Conditional with ???? ???? Polymorphism #2 ??? ??? ?? ?? ??? ???? ???? ??? ?? ?? 81
  • 82. Introduce Null Object ???? ???? ???? ?(null) ??? ????? ?? ?? if (customer == null) plan = BillingPlan.basic(); else plan = customer.getPlan(); - null ?? null ??? ????. - ???? ???, ??? ??? ?? ? ?? ??? ?? ?? ??? ???? ??? ?? ? ??? ???? ???. ??? ? ??? ??? ??? ??? ????. 82
  • 83. Preserve Whole Object ???? ???? ?? ???? ?? ?? ?? ?? ?? ??? ?? ? ????? ?? ?? int low = daysTempRange().getLow(); int high = daysTempRange().getHigh(); withinPlan = plan.withinRange(low, high); withinPlan = plan.withinRange(daysTempRange()); - ???? ??? ?? ?? ???? ??, ?? ????? ? ??? ?? ??? ????. ?? ???? ??? ??? ???? ?? ???? ???? ??? ???? ??. 83
  • 84. Collapse Hierarchy ???? ???? ?? ???? ?? ???? ?? ??? ?? ?? - ????? ???? ?? ??? ?? ????. 84
  • 85. Remove Parameter ???? ???? ????? ? ?? ??? ??? ??? ?? ?? - ?? ?? ????? ?? ??? ?? ???? ????. - ?, ???? ???? ???? ??, ???? ??? ? ? ???? ??. 85
  • 86. Introduce Parameter Object ???? ???? ?? ?? ??? ?????? ??? ?? ?? - ???? ??? ?? ???? ????. - ??, ??? ??? ?? ??? ??? ??? ?? ?????? ????? ???? ??. 86
  • 87. Replace Data Value with Object ???? ???? ???? ???? ??? ??? ??? ??? ?? ?? - ?? ??, ????? ?? ???? ???(String)?? ?? ? ? ???, ???? ???? ?? ????, ? ? ??? ???? ?? ?? ??? ??? ????? ??? ????. 87
  • 88. Replace Array with Object ???? ???? ??? ?? ??? ?? ?? ??? ?? ?? String[] row = new String[3]; row [0] = "Liverpool"; row [1] = "15"; Performance row = new Performance(); row.setName("Liverpool"); row.setWins("15"); 88