[[TableOfContents]] = Chapter 6 Composing Methods = == Extract Method p110 == * 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.'' * 하나로 묶을 수 있는 작은 코드 조각들에 대해서, 그 목적을 잘 표현하는 이름을 가진 메소드에 넣자. {{{~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); } }}} == Inline Method p117 == * 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.'' {{{~cpp int getRating(){ return (moreThanFiveLateDeliveries())?2:1; } boolean moreThanFiveLateDeliveries(){ return _numberOfLateDeliveries > 5; } }}} {{{~cpp 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. [[BR]] ''Replace all references to that temp with the expression.'' {{{~cpp double basePrice = anOrder.basePrice(); return (basePrice > 1000) }}} {{{~cpp return (anOrder.BasePrice() > 1000) }}} == Replace Temp with Query p120 == ["ReplaceTempWithQuery"] == Introduce Explaining Variable p124 == * 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.'' {{{~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 } }}} == 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. [[BR]] ''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); }}} == 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; }}} == 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)''. [[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.'' {{{~cpp 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. [[BR]] ''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"]