Refactoring

1,563 views

Published on

Refactoring.

Published in: Education, Technology, Design
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,563
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
82
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Refactoring

  1. 1. Clean Code<br />Refactoring<br />1<br />
  2. 2. Refactoring<br />Any fool can write code that a computer can understand. Good programmers write code that humans can understand.<br />2<br />
  3. 3. Refactoring<br />What is Refactoring?<br />Martin Fowler : "a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior“.<br />William C. Wake : "Refactoring is the art of safely improving the design of existing code“.<br />3<br />
  4. 4. Refactoring<br />The process of refactoring involves the removal of duplication, the simplification of complex logic, and the clarification of unclear code.<br />The goal of refactoring is NOT to add new functionality<br />The goal is refactoring is to make code easier to maintain in the future<br />4<br />
  5. 5. Refactoring<br />When Should You Refactor?<br /><ul><li>Refactor When You Add Function
  6. 6. Refactor When You Need to Fix a Bug
  7. 7. Refactor As You Do a Code Review</li></ul>5<br />
  8. 8. Refactoring<br />The Refactoring Cycle<br />Choose the worst smell<br />Select a refactoring that will address the smell<br />Apply the refactoring<br />6<br />
  9. 9. Refactoring<br />Bad Smells in Code<br /><ul><li> Duplicated Code
  10. 10. Long Method
  11. 11. Large Class
  12. 12. Long Parameter List
  13. 13. Divergent Change
  14. 14. Shotgun Surgery
  15. 15. Feature Envy
  16. 16. Data Clumps
  17. 17. Primitive Obsession
  18. 18. Switch Statements
  19. 19. Parallel Inheritance Hierarchies
  20. 20. Lazy Class
  21. 21. Speculative Generality
  22. 22. Temporary Field
  23. 23. Message Chains
  24. 24. Middle Man</li></ul>7<br />
  25. 25. Refactoring<br />Bad Smells in Code<br /><ul><li> Inappropriate Intimacy
  26. 26. Alternative Classes with Different Interfaces
  27. 27. Incomplete Library Class
  28. 28. Data Class
  29. 29. Refused Bequest
  30. 30. Comments</li></ul>8<br />
  31. 31. void printOwing() { printBanner(); //print details System.out.println ("name: " + _name); System.out.println ("amount " + getOutstanding()); }<br />Refactoring<br />Extract Method<br />void printOwing() { <br />printBanner(); <br />//print details <br />System.out.println ("name: " + _name); System.out.println ("amount " + getOutstanding()); <br /> }<br />Reusability<br />High Cohesion<br />Readability<br />void printOwing() { <br />printBanner(); <br />printDetails(getOutstanding()); <br /> } <br />Void printDetails (double outstanding) { <br />System.out.println ("name: " + _name); <br />System.out.println ("amount " + outstanding); <br />}<br />Polymorphism<br />9<br />
  32. 32. Refactoring<br />Move Method<br />10<br />
  33. 33. Refactoring<br />Move Method<br />low Coupling<br />High Cohesion<br />11<br />
  34. 34. Refactoring<br />Encapsulate Field<br />you should never make your data public. When you make data public, other objects can change and access data values without the owning object's knowing about it.<br />12<br />
  35. 35. Refactoring<br />Extract Interface<br />13<br />
  36. 36. Refactoring<br />Remove Parameter<br />A parameter is no longer used by the method body. Remove it.<br />14<br />
  37. 37. Refactoring<br />Replace Nested Conditional with Guard Clauses<br />A method has conditional behavior that does not make clear the normal path of execution.<br />Use guard clauses for all the special cases.<br />Readability<br />15<br />
  38. 38. Refactoring<br />Visit our Page on Facebook!<br />16<br />

×