1 / 13

Refactoring: Improving the Quality of Existing Code

Refactoring: Improving the Quality of Existing Code. When and How to Refactor? Refactoring Patterns. Stefan Dobrev. Telerik Corporation. www.telerik.com. What is refactoring?. Improving the design and quality of existing source code without changing its external behavior – Martin Fowler

shasta
Download Presentation

Refactoring: Improving the Quality of Existing Code

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Refactoring: Improving the Quality of Existing Code When and How to Refactor? Refactoring Patterns Stefan Dobrev Telerik Corporation www.telerik.com

  2. What is refactoring? • Improving the design and quality of existing source code without changing its external behavior – Martin Fowler • Step by step process that turns the bad code into good code Source: http://www.flickr.com/photos/pragdave/173640462/

  3. Why we need refactoring? • Improve the design of software • Make software easy to understand • Find out bugs • Minimize/pay technical debt • React to changes • Code constantly changes and its quality constantly degrades (unless refactored) • Requirements often change and code needs to be changed to follow them

  4. When to refactor? • Bad smells in the code indicate need of refactoring • Refactor when you • Add a new method or class • Fix an existing bug • Reviewing someone else’s code • Unit tests guarantee that refactoring does not change the behavior • If there are no unit tests, write them

  5. Code Smells Duplicated code Long method Large class Long parameter list Switch statements Temporary field Bad comments Improper naming Global variables Divergent change Shotgun surgery Feature envy Overused primitives Middle man Speculative generality Inappropriate Intimacy

  6. Data Level Refactorings • Replace a magic number with a named constant • Rename a variable with more informative name • Move an expression inline • Replace an expression with a method • Introduce an intermediate variable • Convert a multi-use variable to a multiple single-use variables • Change an array to an object • Encapsulate a collection

  7. Statement Level Refactorings • Decompose a boolean expression • Move a complex boolean expression into a well-named boolean function • Return as soon as you know the answer instead of assigning a return value • Use break or return instead of a loop control variable • Replace conditionals with polymorphism • Create and use null objects instead of testing for null values

  8. Method Level Refactorings • Extract method • Inline method • Add / Remove parameter • Separate query operations from modification ones • Combine similar methods by parameterzing them • Separate methods whose behavior depends on parameters passed in • Pass a whole object rather than specific fields • Encapsulate downcasting

  9. Class Level Refactorings • Change value objects to reference objects and vice versa • Replace virtual routines with data initialization • Pull members up / Push members down the object hierarchy • Extract specialized code into a subclass • Combine similar code into a superclass • Encapsulate read-only members

  10. Class Interface Refactorings • Move a method to another class • Convert one class to two • Delete a class • Hide a delegating class • Remove the man in the middle • Introduce (use) an extension class • Encapsulate an exposed member variable

  11. System Level Refactorings • Move class (set of classes) to another namespace • Provide a factory method instead of a simple constructor • Replace error codes with exceptions • Extract strings to resource files

  12. Refactoring tools in practice Demo

  13. Code Refactoring ? Questions? ? ? ? ? ? ? ? ? ? http://academy.telerik.com

More Related