Bad Smell in Codes - Part 1


Published on

This will help you to identify the scope to refactor your code. Compiled from Refactoring: Improving the Design of Existing Code by Martin Fowler et al.

Published in: Technology, Health & Medicine
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Bad Smell in Codes - Part 1

  1. 1. Bad smell in codes – Part 1 If it stinks change it. Presented By: Fuad Bin Omar Rashed Kibria [email_address] [email_address]
  2. 2. Smells to be covered <ul><li>Duplicated code </li></ul><ul><li>Long method </li></ul><ul><li>Long parameter list </li></ul><ul><li>Divergent change </li></ul><ul><li>Shotgun surgery </li></ul>
  3. 3. Duplicated Code <ul><li>Smell </li></ul><ul><li>Same code structure in more than one place. </li></ul><ul><li>Refactor </li></ul><ul><li>Extract method and invoke it from both the places. </li></ul>
  4. 4. Extract Method <ul><li>You have a code fragment that can be grouped together. </li></ul><ul><li>Turn the fragment into a method whose name explains the purpose of the method. </li></ul>
  5. 5. Extract Method (Contd.) <ul><li>void printOwing() </li></ul><ul><li>{ </li></ul><ul><li>printBanner(); </li></ul><ul><li>//print details </li></ul><ul><li>System.out.println (&quot;name: &quot; + _name); </li></ul><ul><li>System.out.println (&quot;amount &quot; + getOutstanding()); </li></ul><ul><li>} </li></ul>
  6. 6. Extract Method (Contd.) <ul><li>void printOwing() </li></ul><ul><li>{ </li></ul><ul><li>printBanner(); </li></ul><ul><li>printDetails(getOutstanding()); </li></ul><ul><li>} </li></ul><ul><li>void printDetails (double outstanding) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println (&quot;name: &quot; + _name); </li></ul><ul><li>System.out.println (&quot;amount &quot; + outstanding); </li></ul><ul><li>} </li></ul>
  7. 7. Duplicated Code <ul><li>Smell </li></ul><ul><li>same expression in two sibling subclasses </li></ul><ul><li>Refactor </li></ul><ul><li>Extract method in both classes. </li></ul><ul><li>Pull up field . </li></ul>
  8. 8. Pull Up Field <ul><li>Two subclasses have the same field. </li></ul><ul><li>Move the field to the superclass. </li></ul>
  9. 9. Pull Up Field (Contd.)
  10. 10. Duplicated Code <ul><li>Smell </li></ul><ul><li>Code is similar but not the same </li></ul><ul><li>Refactor: </li></ul><ul><li>Use Extract Method to </li></ul><ul><li>separate the similar bits from the different bits. </li></ul><ul><li>Use Form Template Method . </li></ul>
  11. 11. Form template method <ul><li>You have two methods in subclasses that perform similar steps in the same order, yet the steps are different. </li></ul><ul><li>Get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up. </li></ul>
  12. 12. Form Template Method (Contd.)
  13. 13. Duplicated Code <ul><li>Smell </li></ul><ul><li>Methods do the same thing with a different algorithm </li></ul><ul><li>Refactor: </li></ul><ul><li>Choose the clearer of the two algorithms Use </li></ul><ul><li>Use Substitute Algorithm. </li></ul>
  14. 14. Substitute Algorithm <ul><li>You want to replace an algorithm with one that is clearer. </li></ul><ul><li>Replace the body of the method with the new algorithm. </li></ul>
  15. 15. Substitute Algorithm(Contd.) <ul><li>String foundPerson(String[] people) </li></ul><ul><li>{ </li></ul><ul><li>for (int i = 0; i < people.length; i++) </li></ul><ul><li>{ </li></ul><ul><li>if (people[i].equals (&quot;Don&quot;)){ return &quot;Don&quot;; } </li></ul><ul><li>if (people[i].equals (&quot;John&quot;)){ return &quot;John&quot;; } </li></ul><ul><li>if (people[i].equals (&quot;Kent&quot;)){ return &quot;Kent&quot;; } </li></ul><ul><li>} </li></ul><ul><li>return &quot;&quot;; </li></ul><ul><li>} </li></ul>
  16. 16. Substitute Algorithm (Contd.) <ul><li>String foundPerson(String[] people) </li></ul><ul><li>{ </li></ul><ul><li>List candidates = Arrays.asList(new String[] {&quot;Don&quot;, &quot;John&quot;, &quot;Kent&quot;}); </li></ul><ul><li>for (int i=0; i<people.length; i++) </li></ul><ul><li>if (candidates.contains(people[i])) </li></ul><ul><li>return people[i]; return &quot;&quot;; </li></ul><ul><li>} </li></ul>
  17. 17. Duplicated Code <ul><li>Smell </li></ul><ul><li>Duplicated code in two unrelated class. </li></ul><ul><li>Refactor: </li></ul><ul><li>Use Extract Class in one class. </li></ul><ul><li>Use the new component to the other. </li></ul><ul><li>Decide where the method makes sense. </li></ul>
  18. 18. Extract Class <ul><li>You have one class doing work that should be done by two. </li></ul><ul><li>Create a new class and move the relevant fields and methods from the old class into the new class. </li></ul>
  19. 19. Extract Class (Contd.)
  20. 20. Long method <ul><li>Object program having short methods live best and longest. </li></ul><ul><li>Little methods are the most valuable. </li></ul><ul><li>Longer methods are difficult to understand. </li></ul>
  21. 21. Long method <ul><li>Give methods a good name. </li></ul><ul><li>Whenever you feel the need to comment something make it a method. </li></ul><ul><ul><li>Group of lines </li></ul></ul><ul><ul><li>Even if it is a single line </li></ul></ul><ul><ul><li>Even if method call is longer than code itself. </li></ul></ul><ul><ul><li>Method length is not the key here. </li></ul></ul><ul><ul><li>What the method does and how it does it is important. </li></ul></ul>
  22. 22. Long method <ul><li>Extract Method </li></ul>
  23. 23. Long method <ul><li>Smell </li></ul><ul><li>Use of temporary variable. </li></ul><ul><li>Refactor </li></ul><ul><li>Replace temp with query. </li></ul>
  24. 24. Replace temp with query <ul><li>You are using a temporary variable to hold the result of an expression. </li></ul><ul><li>Extract the expression into a method. Replace all references to the temp with the expression. The new method can then be used in other methods </li></ul>
  25. 25. Replace temp with query <ul><li>double basePrice = _quantity * _itemPrice; </li></ul><ul><li>if (basePrice > 1000) </li></ul><ul><li>return basePrice * 0.95; </li></ul><ul><li>else </li></ul><ul><li>return basePrice * 0.98; </li></ul>
  26. 26. Replace temp with query <ul><li>if (basePrice() > 1000) </li></ul><ul><li>return basePrice() * 0.95; </li></ul><ul><li>else return basePrice() * 0.98; </li></ul><ul><li>... </li></ul><ul><li>... </li></ul><ul><li>double basePrice() </li></ul><ul><li>{ </li></ul><ul><li>return _quantity * _itemPrice; </li></ul><ul><li>} </li></ul>
  27. 27. Long method <ul><li> Smell </li></ul><ul><ul><li>Methods with long list of parameters. </li></ul></ul><ul><ul><li>Refactor </li></ul></ul><ul><ul><li>Introduce Parameter Object </li></ul></ul><ul><ul><li>Preserve Whole Object </li></ul></ul><ul><ul><li>Method with method Object </li></ul></ul>
  28. 28. Introduce Parameter Object <ul><li>You have a group of parameters that naturally go together. </li></ul><ul><li>Replace them with an object. </li></ul>
  29. 29. Introduce Parameter Object
  30. 30. Preserve whole object <ul><li>You are getting several values from an object and passing these values as parameters in a method call. </li></ul><ul><li>Send the whole object instead. </li></ul>
  31. 31. Preserve whole object <ul><li>int low = daysTempRange().getLow(); </li></ul><ul><li>int high = daysTempRange().getHigh(); withinPlan = plan.withinRange(low, high); </li></ul>
  32. 32. Preserve whole object <ul><li>withinPlan = plan.withinRange(daysTempRange()); </li></ul>
  33. 33. Replace method with method object <ul><li>You have a long method that uses local variables in such a way that you cannot apply Extract Method </li></ul><ul><li>Turn the method into its own object so that all the local variables become fields on that object. You can then decompose the method into other methods on the same object. </li></ul>
  34. 34. Replace method with method object <ul><li>class Order </li></ul><ul><li>{ </li></ul><ul><li>double price() </li></ul><ul><li>{ </li></ul><ul><li>double primaryBasePrice; </li></ul><ul><li>double secondaryBasePrice; </li></ul><ul><li>double tertiaryBasePrice; </li></ul><ul><li>// long computation; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  35. 35. Replace method with method object
  36. 36. Long method <ul><li>Smell </li></ul><ul><li>Too many conditions and loops </li></ul><ul><li>Refactor </li></ul><ul><li>With loops, extract the loop and the code within the loop into its own method. </li></ul><ul><li>Use Decompose Conditional. </li></ul>
  37. 37. Decompose Conditional <ul><li>You have a complicated conditional (if-then-else) statement. </li></ul><ul><li>Extract methods from the condition, then part, and else parts </li></ul>
  38. 38. Decompose Conditional <ul><li>if (date.before (SUMMER_START) || date.after(SUMMER_END)) </li></ul><ul><li>charge = quantity * _winterRate + _winterServiceCharge; </li></ul><ul><li>else </li></ul><ul><li>charge = quantity * _summerRate; </li></ul>
  39. 39. Decompose Conditional <ul><li>if (notSummer(date)) </li></ul><ul><li>charge = winterCharge(quantity); </li></ul><ul><li>else </li></ul><ul><li>charge = summerCharge (quantity); </li></ul>
  40. 40. Long Parameter List <ul><li>Smell </li></ul><ul><li>A method call requires passing long list of parameters. </li></ul><ul><li>Refactor </li></ul><ul><li>Use Replace Parameter with Method, Preserve whole object or Introduce Parameter Object. </li></ul>
  41. 41. Replace Parameter With Method int basePrice = _quantity * _itemPrice; discountLevel = getDiscountLevel(); double finalPrice = discountedPrice (basePrice,discountLevel); int basePrice = _quantity * _itemPrice; double finalPrice = discountedPrice(basePrice);
  42. 42. Divergent Change <ul><li>Smell </li></ul><ul><li>One class is commonly changed in different ways for different reasons. </li></ul><ul><li>Refactor </li></ul><ul><li>Use Extract class by identifying everything that changes for a particular cause and put them all together. </li></ul>
  43. 43. Shotgun Surgery <ul><li>Smell </li></ul><ul><li>A small changes in the code force changes in different classes. </li></ul><ul><li>Refactor </li></ul><ul><li>Use Move Method and Move Field to put all the changes into a single class. </li></ul><ul><li>If no current class looks like a good candidate, create one. </li></ul><ul><li>use Inline Class to bring a whole bunch of behavior together. </li></ul>
  44. 44. Move Method <ul><li>A method is, or will be, using or used by more features of another class than the class on which it is defined. </li></ul><ul><li>Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether. </li></ul>
  45. 45. Introduce Parameter Object
  46. 46. Move Field <ul><li>A field is, or will be, used by another class more than the class on which it is defined. </li></ul><ul><li>Create a new field in the target class, and change all its users. </li></ul>
  47. 47. Introduce Parameter Object
  48. 48. To be continued..
  49. 49. References 1. Refactoring: Improving the Design of Existing Code By Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts 2.