Download presentation
Presentation is loading. Please wait.
Published byΓαпїЅα Ζαχαρίου Modified over 6 years ago
1
Refactoring with inline temp, method, and class
Sung-Jun Lee
2
Inline temp Example: You have a temp that is assigned to once with a simple expression. double basePrice = anOrder.basePrice(); return (basePrice > 1000) Motivation: used in Replace temp with query
3
Inline temp(cont.) Solution: Replace all references to temps that are assigned once with a simple expression with the expression itself. return (anOrder.basePrice() > 1000) Benefits: Fixes problems that need to be fixed to do other types of refactoring
4
Inline Method Example: Make method name as clear as the body int getRating() { return (moreThanFiveLateDeliveries()) ? 2 : 1;} boolean moreThanFiveLateDeliveries(){ return _numberOfLateDeliveries > 5;} Motivation: To get rid of pointless indirection and often good to do before other refactoring types such as replacing method with method object
5
Inline Method(cont.) Solution: Put the method’s body into the body of its callers and remove the method. int getRating() { return (_numberOfLateDeliveries > 5)? 2 : 1; } Benefits: Makes the code clear and easy to read
6
Inline Class Example: A class is not doing much class Person... public String getName() { return _name;} public String getTelephoneNumber(){ return _officeTelephone.getTelephoneNumber(); private String _name; private TelephoneNumber _officeTelephone = new TelephoneNumber();
7
Inline class(cont.) class TelephoneNumber... public String getTelephoneNumber() { return ("(" + _areaCode + ") " + _number); } String getAreaCode() {return _areaCode;} void setAreaCode(String arg) {_areaCode = arg;} String getNumber() {return _number;} void setNumber(String arg) {_number = arg;} private String _number; private String _areaCode;
8
Inline Class(cont.) Motivation: Reverse of extract class. Result of other refactoring methods Solutions:Move the features of class to another class then delete the old class class Person... String getAreaCode() {return _officeTelephone.getAreaCode();} void setAreaCode(String arg) {_officeTelephone.setAreaCode(arg);} String getNumber() {return _officeTelephone.getNumber();} void setNumber(String arg) {_officeTelephone.setNumber(arg);} Person martin = new Person(); martin.getOfficeTelephone().setAreaCode ("781"); becomes Person martin = new Person(); martin.setAreaCode ("781");
9
Inline Class(cont.) Benefits: Makes the program easier to read.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.