12. Code smell? Too many method params public void calculate ( int from , int to , int x , int y , int ... ){} Ungrouped related fields class DayCalculator { private Date to; private Date from; private boolean skipSaturday; private boolean skipSunday; ..... } God classes class ManagerServiceHelperImpl { public BigInt calculatePrice () { //calculate stuff } public void handleResponse (){ //handle stuff } public Report manageReportPrinter (){ // manage stuff } public boolean isCanDoEverything () { return true; } }
13. Code smell? Unused/temporary object fields public class SessionVO { private PartnerFeatureVO partnerFeatureVO = null ; private ReserveCertificateVO reserveCertificateVO; // only used for claim private boolean giftOrTransfer; ...... ...... } Complicated VOs and Data Classes public class Session VO { // contains 20 different nested mutable objects // contains over 100 variables // unknown lifecycle }
14. Code smells (cont'd) Numerous switch/if/else statements public double calculateArea ( Object shape ) { double area = 0 ; if (shape instanceof Square) { Square square = ( Square ) shape ; area = square. getA () * square. getA () ; } else if (shape instanceof Rectangle) { Rectangle rectangle = ( Rectangle ) shape ; area = rectangle. getA () * rectangle. getB () ; } } else if (shape instanceof Circle) { Circle circle = ( Circle ) shape ; area = Math . PI * cirle. getR () * cirle. getR () ; } return area ; } Deep method chains getHelper(){ getHandlerHelper(){ handleResponse(){ processReport();{ } } } }
15.
16.
17. How to refactor: catalog of techniques http://refactoring.com/catalog/index.html
18. How to refactor: some techniques Rename Method class DistanceCalculator (){ // FIXME: rename this method public int process(Coordinates coords){ //this method calculates the distance between 2 coordinates ..... . ..... } } class DistanceCalculator (){ public int calculateDistance(Coordinates coords){ ..... . ..... } }
19. How to refactor: some techniques Extract Interface class Employee (){ double getRate(){ ... } boolean hasSpecialSkill(){ ... } getName (){ ... } getDepartment(){ ... } } interface Billable{ double getRate(); boolean hasSpecialSkill(); } class Employee implements Billable() {} class Contractor implements Billable() {}
20. How to refactor: some techniques Introduce Parameter Object public void calculate ( int from , int to , int x , int y ){ ... } public void calculate ( Range range , Location location ){ ... }
21. How to refactor: some techniques Replace Conditional with Polymorphism public class Client { public double calculateArea ( Object shape ) { double area = 0 ; if (shape instanceof Square) { Square square = ( Square ) shape ; area = square. getA () * square. getA () ; } else if (shape instanceof Rectangle) { Rectangle rectangle = ( Rectangle ) shape ; area = rectangle. getA () * rectangle. getB () ; } } else if (shape instanceof Circle) { Circle circle = ( Circle ) shape ; area = Math . PI * cirle. getR () * cirle. getR () ; } return area ; } } public class Square implements Shape { private double a ; ... public double getArea () { return a * a ; } } public class Rectangle implements Shape { private double a; private double b; ... public double getArea () { return a * b; } } public class Client { private Shape shape; ... public double calculateArea() { return shape.getArea(); } }
22. How to refactor: some techniques Replace Type Code with State/Strategy
23. How to refactor: some techniques Replace Inheritance with Delegation