Your SlideShare is downloading. ×
Refactoring
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Refactoring

773
views

Published on


0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
773
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
32
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Refactoring Dhaval Dalal software-artisan.com @softwareartisan
  • 2. dhaval.dalal@software-artisan.com Refactoring Metaphor John Thompson, the hatter makes and sells hats for ready money!
  • 3. dhaval.dalal@software-artisan.com Refactoring Metaphor John Thompson, the hatter makes and sells hats for ready money!
  • 4. dhaval.dalal@software-artisan.com Refactoring Metaphor John Thompson, the hatter makes and sells hats for ready money!
  • 5. dhaval.dalal@software-artisan.com Refactoring Metaphor John Thompson, the hatter makes and sells hats for ready money!
  • 6. dhaval.dalal@software-artisan.com Refactoring Metaphor John Thompson
  • 7. dhaval.dalal@software-artisan.com What is Refactoring? Refactoring means improving the design after it has been written. Martin Fowler.
  • 8. dhaval.dalal@software-artisan.com Why Should I Refactor? ! Development is a process of Discovery and code constantly evolves. ! Refactoring is a code curative technique, just as Sleep is a Curative Agency for the body playing part in repairing mechanism of the body. 5
  • 9. dhaval.dalal@software-artisan.com Why Should I Refactor? Any fool can write code that a computer can understand. Good programmers write code that humans can understand. The purpose of refactoring is to make the software easier to understand and modify. Martin Fowler.
  • 10. dhaval.dalal@software-artisan.com Why Should I Refactor? ! It improves the design of software " By repairing a highly coupled, non-cohesive design. ! Helps to find bugs faster. ! Helps to Program Faster.
  • 11. dhaval.dalal@software-artisan.com Code Smells…lets code along ! Duplicate Code ! Switch Statements ! Long Method ! Long Parameter List ! Large Class ! Indecent Exposure ! Divergent Change ! Shotgun Surgery ! ...and many more. If it Stinks, Change it! ! Speculative Generality Kent Beck
  • 12. dhaval.dalal@software-artisan.com Deodorize With Fresheners…code along ! Encapsulate Collections ! Encapsulate Downcast ! Replace Error Code with Exception ! Introduce Variable ! Introduce Symbolic Constants ! Rename Method/Variable ! Remove Control Flag ! Simplify Conditionals ! Simply Nested Conditionals with Guard Clauses ! Move Method ! Chain Multiple Constructors/Methods ! Replace Type Code with Enums or Class
  • 13. dhaval.dalal@software-artisan.com Stronger Deodorizing Agents… ! Replace Array with Object ! Replace conditionals with Polymorphism ! Replace Type Code with State/Strategy ! Introduce Null Object ...and many more
  • 14. dhaval.dalal@software-artisan.com General Principles in Refactoring ! From Near to Far. " That which is obvious and close to us now is the starting point, a.k.a the inflection point. " That which is abstract is the goal. ! Take baby-steps. 11
  • 15. dhaval.dalal@software-artisan.com Refactoring Cycle 12 Choose on appropriate Refactoring to apply Apply Refactoring Run ALL Tests (Get GREEN Bar) Reached Desired Structure? Yes No
  • 16. dhaval.dalal@software-artisan.com Undertaking Big Refactorings (Bridge Replacement Metaphor) ! Let traffic continue to flow on old bridge. ! Create new refactored implementation. 13 ! Create another bridge side-by-side the old one. ! Take an Interface-oriented approach. ! House the old code in one implementation. ! When the new bridge is ready, delink traffic from the old one and re-route it over the new bridge. ! Use the new impl. by replacing the old one.
  • 17. dhaval.dalal@software-artisan.com Refactoring To Patterns ! Arrive to pattern via series of small refactorings. " Rigorously apply OO principles along-side continuous refactoring. # SRP, OCP, LSP, ISP, DIP (SOLID) etc... " You will eventually evolve to some known pattern. ! When you “intuit” that a pattern will readily apply. " Approach this in baby steps. 14
  • 18. dhaval.dalal@software-artisan.com Refactoring and Legacy Code ! Code without tests is Legacy. " Does anyone still create this? " Michael Feathers’ has an excellent book on this. #Working Effectively with Legacy Code ! Refactoring code without tests is like performing a tight-rope walk without any safety net underneath. 15
  • 19. dhaval.dalal@software-artisan.com Motivators and Inhibitors
  • 20. dhaval.dalal@software-artisan.com As in many things the answer lies in balancing opposing forces, especially when its difficult to make hidden Business Value explicit. Pragmatic Vs Dogmatic Agility Vs Fragility 17 When to Refactor?
  • 21. dhaval.dalal@software-artisan.com Views on Refactoring ! A traditionalist view on Refactoring is re-work! ! In reality, it is responding to new information as it arrives. ! At the same time, avoid “Major Refactorings” " Do initial architecture envisioning. 18
  • 22. dhaval.dalal@software-artisan.com No Silver Bullet Refactoring is no “silver bullet”, yet it is a valuable tool, a pair of silver pliers that helps you keep good grip on your code. Without refactoring, the design of the program will decay. Martin Fowler
  • 23. dhaval.dalal@software-artisan.com References ! Refactoring " Martin Fowler. ! Refactoring to Patterns " Joshua Kerievsky. ! Refactoring Motivations " Jay Fields’ Blog ! Creating an Agile Culture to drive organizational Change " David Anderson
  • 24. Thank You! Dhaval Dalal dhaval.dalal@software-artisan.com

×