U E D R , A S I H C RSS

Refactoring/Composing Methods

1. Chapter 6 Composing Methods

1.1. 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.

  • 하나로 묶을 수 있는 작은 코드 조각들에 대해서, 그 목적을 잘 표현하는 이름을 가진 메소드에 넣자.

~cpp 
    void printOwing(double amount){
        printBanner();
        // print details
        System.out.println( "name:" + _name);
        System.out.println( "amount" + amount);
    }

~cpp 
    void printOwing(double amount){
        printBanner();
        // print details
        printDetails( amount );
    }
    void printDetails (double amount){
        System.out.println( "name:" + _name);
        System.out.println( "amount" + amount);
    }

1.2. 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.

~cpp 
    int getRating(){
        return (moreThanFiveLateDeliveries())?2:1;
    }
    boolean moreThanFiveLateDeliveries(){
        return _numberOfLateDeliveries > 5;
    }

~cpp 
    int getRating(){
        return (_numberOfLateDeliveries>5)?2:1;
    }

1.3. 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.

~cpp 
    double basePrice = anOrder.basePrice();
    return (basePrice > 1000)
~cpp 
    return (anOrder.BasePrice() > 1000)

1.4. Replace Temp with Query p120

1.5. 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.

~cpp 
    if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
         (browser.toUpperCase().indexOf("IE") > -1) &&
         wasInittialized() && resize > 0)
    {
        // do something
    }

~cpp 
    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
    }

1.6. 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.

~cpp 
    double temp = 2 * (_height + _width);
    System.out.println (temp);
    temp = _height * _width;
    System.out.println(temp);

~cpp 
    final double perimeter = 2 * (_height + width);
    System.out.println(perimeter);
    final dougle area = _height * _width;
    System.out.println(area);

1.7. Remove Assignments to Parameters p131

  • The code assigns to a parameter. Use a temporary variagle instead.

~cpp 
    int descount ( int inputVal, int Quantity, int yearToDate) {
        if (inputVal > 50) inputVal -= 2;
    
~cpp 
    int descount ( int inputVal, int Quantity, int yearToDate) {
        int result = inputVal;
        if (inputVal > 50) return -= 2;

1.8. 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.

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

1.9. Substititude Algorithm p139

  • You want to replace an altorithm with one that is clearer.
    Replace the body of the method with the new altorithm.

~cpp 
    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 "";
    }

~cpp 
    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
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:53
Processing time 0.0239 sec