Refactoring

8,653 views

Published on

Presentation from "Anonymous Developers Club" gathering in Kiev on "Refactoring" topic

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,653
On SlideShare
0
From Embeds
0
Number of Embeds
6,614
Actions
Shares
0
Downloads
3
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide
  • We need to realize that software must be soft: it has to be easy to change because it will change despite our misguided efforts otherwise.
  • • Make a small change (a single refactoring)• Run all the tests to ensure everything stillworks• If everything works, move on to the nextrefactoring• If not, fix the problem, or undo the change,so you still have a working system
  • Any time you find that you can improve the design of existing codeYou detect a code that smellsAfter static analysis
  • Any time you find that you can improve the design of existing codeYou detect a code that smellsAfter static analysis
  • Refactoring

    1. 1. REFACTORING<br />XP Injection<br />http://xpinjection.com<br />
    2. 2. Do You Know Your Code?<br />
    3. 3. Developer Golden Rule<br />Any fool can write code that a computer can understand<br />
    4. 4. Good Developer Golden Rule<br />Any fool can write code that a computer can understand<br />Good programmers write code that humans can understand<br />
    5. 5. Why Fix What is Not Broken?<br />
    6. 6. If the Code is Hard to Understand<br />Hard to maintain<br />Hard to improve<br />Hard to work with evolutionary design<br />More expensive<br />
    7. 7. What is Refactoring?<br />A series of small steps, each of which changes the program’s internal structure without changing its external behavior - Martin Fowler<br />Verify no change in external behavior by<br />Testing<br />Using the right tool (IDE)<br />Formal code analysis by tool<br />Code review or pair programming<br />Being very, very careful<br />
    8. 8. Prerequisites for Success<br />Safety net<br />Everybody agreed “Collective code ownership”<br />Common understanding of refactoring methods<br />Supportive environment<br />Powerful IDE<br />
    9. 9. Refactoring Cycle<br />Find smell<br />Select <br />refactoring<br />Apply <br />refactoring<br />Run <br />all tests<br />
    10. 10. 3 Ways to Start<br />
    11. 11. Find Something to Improve<br />
    12. 12. Make Static Analysis<br />
    13. 13. Find Smell<br />
    14. 14. How to Find Smell<br />
    15. 15. Smells<br />
    16. 16. Main Goals<br />Organizing data<br />Composing methods<br />Making methods calls simpler<br />Moving features between objects<br />Simplifying conditional expression<br />Dealing with generalizations<br />
    17. 17. Operations Associated with Refactoring<br />Replace<br />Extract<br />Move<br />Change<br />Introduce<br />Remove<br />Pull<br />Consolidate<br />Push<br />Inline<br />Add<br />
    18. 18. When to Refactor?<br />When add new functionality<br />Refactor existing code until you understand it<br />Refactor the design to make it simple to add<br />When try to find bugs<br />Refactor to understand the code<br />During code reviews<br />Immediate effect of code review<br />Allows for higher level suggestions<br />
    19. 19. Key Elementsof Refactoring<br />Simplifying complex statements<br />Abstracting common solutions into reusable code<br />Removal of duplicate code<br />
    20. 20. Refactoring Techniques<br />
    21. 21. 6 Techniques in Action<br />
    22. 22. Extract Method<br />void printOwing() {<br />printBanner();<br /> //print details<br />System.out.println("name: " + _name);<br />System.out.println("amount " + getOutstanding());<br />}<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 />
    23. 23. Extract Super Class<br />
    24. 24. Decompose Conditional<br />if (date.before (SUMMER_START) || <br />date.after(SUMMER_END)) {<br /> charge = quantity * winterRate+ serviceCharge;<br />} else { <br /> charge = quantity * summerRate;<br />}<br />if (notSummer(date)) {<br /> charge = winterCharge(quantity);<br />} else {<br /> charge = summerCharge (quantity);<br />}<br />
    25. 25. Replace Error Code with Exception<br />boolean withdraw(int amount) {<br /> if (amount > balance)<br /> return false;<br /> else {<br /> balance -= amount;<br /> return true;<br /> }<br />}<br />void withdraw(int amount) throws BalanceException {<br /> if (amount > balance) throw new BalanceException();<br /> balance -= amount;<br />}<br />
    26. 26. Replace Exception with Test<br />double getValueForPeriod (int period) {<br /> try {<br /> return values[period];<br /> } catch (ArrayIndexOutOfBoundsException e) {<br /> return 0;<br /> }<br />}<br />double getValueForPeriod (int period) {<br /> return period >= values.length ? 0 : values[period];<br />}<br />
    27. 27. Parameterize Method<br />
    28. 28. Good Design Principles<br />DRY<br />KISS<br />YAGNI<br />
    29. 29. No Silver Bullet<br />Needs lots of practice<br />Requires discipline<br />Continuous usage<br />Little steps<br />
    30. 30. Little steps – huge results<br />
    31. 31. You Need to Pay Technical Debt<br />
    32. 32. Refactoring Benefits<br />More readable code<br />Easier maintenance<br />Code reuse<br />Easier to add new code<br />Improved design<br />Better understanding<br />Speeding knowledge<br />Easier to test<br />Minimize duplication <br />
    33. 33. Live Demo<br />
    34. 34. Questions?<br />

    ×