Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Refactoring/ComposingMethods: Difference between revisions

From ZeroWiki
imported>Unknown
No edit summary
 
(Repair batch-0003 pages from live compare)
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
= Chapter 6 Composing Methods =
= Chapter 6 Composing Methods =
== Extract Method p110 ==
== Extract Method p110 ==
* You have a code fragment that can be grouped together.''Turn the fragment into a method whose name explains the purpose of the method.''
* You have a code fragment that can be grouped together.<br />''Turn the fragment into a method whose name explains the purpose of the method.''


* 하나로 묶을 수 있는 작은 코드 조각들에 대해서, 그 목적을 잘 표현하는 이름을 가진 메소드에 넣자.
* 하나로 묶을 수 있는 작은 코드 조각들에 대해서, 그 목적을 잘 표현하는 이름을 가진 메소드에 넣자.
Line 22: Line 22:
     }
     }
== Inline Method p117 ==
== Inline Method p117 ==
* A method's body is just as clear as its name. ''Put the method's body into the body of its callers and remove the method.''
* A method's body is just as clear as its name. <br /> ''Put the method's body into the body of its callers and remove the method.''
     int getRating(){
     int getRating(){
         return (moreThanFiveLateDeliveries())?2:1;
         return (moreThanFiveLateDeliveries())?2:1;
Line 35: Line 35:


== Inline Temp p119 ==
== Inline Temp p119 ==
* You have a temp that is assigned to once twith a simple expression, and the temp is getting in the way of other refactorings. ''Replace all references to that temp with the expression.''
* You have a temp that is assigned to once twith a simple expression, and the temp is getting in the way of other refactorings. <br /> ''Replace all references to that temp with the expression.''
     double basePrice = anOrder.basePrice();
     double basePrice = anOrder.basePrice();
     return (basePrice &gt; 1000)
     return (basePrice &gt; 1000)
Line 42: Line 42:
[[ReplaceTempWithQuery]]
[[ReplaceTempWithQuery]]
== Introduce Explaining Variable p124 ==
== Introduce Explaining Variable p124 ==
* You have a complicated expression. ''Put the result of the expression, or parts of the expression,in a temporary variagle with a name that explains the purpose.''
* You have a complicated expression. <br /> ''Put the result of the expression, or parts of the expression,in a temporary variagle with a name that explains the purpose.''
     if ( (platform.toUpperCase().indexOf("MAC") &gt; -1) &amp;&amp;
     if ( (platform.toUpperCase().indexOf("MAC") &gt; -1) &amp;&amp;
           (browser.toUpperCase().indexOf("IE") &gt; -1) &amp;&amp;
           (browser.toUpperCase().indexOf("IE") &gt; -1) &amp;&amp;
Line 59: Line 59:


== Split Temprorary Variable p128 ==
== Split Temprorary Variable p128 ==
* You have a temporary variagle assigned to more than once, bur is not a loop variagle nor a collecting temporary variagle. ''Make a separate temporary variagle for each assignment.''
* You have a temporary variagle assigned to more than once, bur is not a loop variagle nor a collecting temporary variagle. <br /> ''Make a separate temporary variagle for each assignment.''
     double temp = 2 * (_height + _width);
     double temp = 2 * (_height + _width);
     System.out.println (temp);
     System.out.println (temp);
Line 80: Line 80:


== Replace Method with Method Object p135 ==
== Replace Method with Method Object p135 ==
* You have a long method that uses local variagles in such a way that you cannot apply ''Extract Method(110)''.  
* You have a long method that uses local variagles in such a way that you cannot apply ''Extract Method(110)''. <br />
''Turn the method into ints own object so that all the local variagles become fields on that object. You can then decompose the method into other methods on the same object.''
''Turn the method into ints own object so that all the local variagles become fields on that object. You can then decompose the method into other methods on the same object.''


Line 94: Line 94:
http://zeropage.org/~reset/zb/data/ReplaceMethodWithMethodObject.gif
http://zeropage.org/~reset/zb/data/ReplaceMethodWithMethodObject.gif
== Substititude Algorithm p139 ==
== Substititude Algorithm p139 ==
* You want to replace an altorithm with one that is clearer. ''Replace the body of the method with the new altorithm.''
* You want to replace an altorithm with one that is clearer. <br /> ''Replace the body of the method with the new altorithm.''
     String foundPerson(String[] people){
     String foundPerson(String&#91;&#93; people){
         for (int i = 0; i &lt; people.length; i++) {
         for (int i = 0; i &lt; people.length; i++) {
             if (people[i].equals ("Don")){
             if (people&#91;i&#93;.equals ("Don")){
                 return "Don";
                 return "Don";
             }
             }
             if (people[i].equals ("John")){
             if (people&#91;i&#93;.equals ("John")){
                 return "John";
                 return "John";
             }
             }
             if (people[i].equals ("Kent")){
             if (people&#91;i&#93;.equals ("Kent")){
                 return "Kent";
                 return "Kent";
             }
             }
Line 110: Line 110:
     }
     }


     String foundPerson(String[] people){
     String foundPerson(String&#91;&#93; people){
         ListCandidates = Arrays.asList(new String[] {"Don", John", "Kent"});
         ListCandidates = Arrays.asList(new String&#91;&#93; {"Don", John", "Kent"});
         for (int i = 0; i &lt; people.length; i++)
         for (int i = 0; i &lt; people.length; i++)
             if (candidates.contains(people[i]))
             if (candidates.contains(people&#91;i&#93;))
                 return people[i];
                 return people&#91;i&#93;;
         return "";
         return "";
     }
     }


----
----
[[Refactoring]]
Refactoring
 

Latest revision as of 00:29, 27 March 2026

Chapter 6 Composing Methods

Extract Method p110

  • You have a code fragment that can be grouped together.
    Turn the fragment into a method whose name explains the purpose of the method.
  • 하나로 묶을 수 있는 작은 코드 조각들에 대해서, 그 목적을 잘 표현하는 이름을 가진 메소드에 넣자.
    void printOwing(double amount){
        printBanner();
        // print details
        System.out.println( "name:" + _name);
        System.out.println( "amount" + amount);
    }
    void printOwing(double amount){
        printBanner();
        // print details
        printDetails( amount );
    }
    void printDetails (double amount){
        System.out.println( "name:" + _name);
        System.out.println( "amount" + amount);
    }

Inline Method p117

  • A method's body is just as clear as its name.
    Put the method's body into the body of its callers and remove the method.
    int getRating(){
        return (moreThanFiveLateDeliveries())?2:1;
    }
    boolean moreThanFiveLateDeliveries(){
        return _numberOfLateDeliveries > 5;
    }
    int getRating(){
        return (_numberOfLateDeliveries>5)?2:1;
    }

Inline Temp p119

  • You have a temp that is assigned to once twith a simple expression, and the temp is getting in the way of other refactorings.
    Replace all references to that temp with the expression.
    double basePrice = anOrder.basePrice();
    return (basePrice > 1000)
    return (anOrder.BasePrice() > 1000)

Replace Temp with Query p120

ReplaceTempWithQuery

Introduce Explaining Variable p124

  • You have a complicated expression.
    Put the result of the expression, or parts of the expression,in a temporary variagle with a name that explains the purpose.
    if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
         (browser.toUpperCase().indexOf("IE") > -1) &&
         wasInittialized() && resize > 0)
    {
        // do something
    }
    final boolean isMacOs = platform.toUpperCase().indexOf("MAX") > -1;
    final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1);
    final boolean wasResized = resize > 0;

    if ( isMaxOs && isIEBrowser && wasResized ) {
        // do something
    }

Split Temprorary Variable p128

  • You have a temporary variagle assigned to more than once, bur is not a loop variagle nor a collecting temporary variagle.
    Make a separate temporary variagle for each assignment.
    double temp = 2 * (_height + _width);
    System.out.println (temp);
    temp = _height * _width;
    System.out.println(temp);
    final double perimeter = 2 * (_height + width);
    System.out.println(perimeter);
    final dougle area = _height * _width;
    System.out.println(area);

Remove Assignments to Parameters p131

  • The code assigns to a parameter. Use a temporary variagle instead.
    int descount ( int inputVal, int Quantity, int yearToDate) {
        if (inputVal > 50) inputVal -= 2;
    
    int descount ( int inputVal, int Quantity, int yearToDate) {
        int result = inputVal;
        if (inputVal > 50) return -= 2;

Replace Method with Method Object p135

  • You have a long method that uses local variagles in such a way that you cannot apply Extract Method(110).

Turn the method into ints own object so that all the local variagles become fields on that object. You can then decompose the method into other methods on the same object.

    class Order ...
        double price() {
            double primaryBasePrice;
            double secondaryBasePrice;
            double teriaryBasePrice;
            // long computation;
            ...
    }

http://zeropage.org/~reset/zb/data/ReplaceMethodWithMethodObject.gif

Substititude Algorithm p139

  • You want to replace an altorithm with one that is clearer.
    Replace the body of the method with the new altorithm.
    String foundPerson(String[] people){
        for (int i = 0; i < people.length; i++) {
            if (people[i].equals ("Don")){
                return "Don";
            }
            if (people[i].equals ("John")){
                return "John";
            }
            if (people[i].equals ("Kent")){
                return "Kent";
            }
        }
        return "";
    }
    String foundPerson(String[] people){
        ListCandidates = Arrays.asList(new String[] {"Don", John", "Kent"});
        for (int i = 0; i < people.length; i++)
            if (candidates.contains(people[i]))
                return people[i];
        return "";
    }

Refactoring