refactoring: improving the quality of existing code

15
Refactoring: Improving the Quality of Existing Code When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation www.telerik. com

Upload: rimona

Post on 09-Feb-2016

30 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Refactoring: Improving the Quality of Existing Code

Refactoring: Improving the

Quality of Existing Code

When and How to Refactor? Refactoring Patterns

Alexander VakrilovTelerik Corporationwww.telerik.

com

Page 2: Refactoring: Improving the Quality of Existing Code

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

2

Source: http://www.flickr.com/photos/pragdave/173640462/

Page 3: Refactoring: Improving the Quality of Existing Code

Why we need refactoring?

Improve the design of software Make software easy to understand Find out bugs 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

Make the code more easy to change 3

Page 4: Refactoring: Improving the Quality of Existing Code

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

them4

Page 5: Refactoring: Improving the Quality of Existing Code

5

Code Smells Duplicated code Long method Large class Long parameter list Switch statements Temporary field Bad comments / Comments for

bad code Improper naming Data members are public

Page 6: Refactoring: Improving the Quality of Existing Code

6

Code Smells(2) Global variables Overused primitives Divergent change Shotgun surgery Feature envy Middle man Speculative generality Inappropriate Intimacy

Page 7: Refactoring: Improving the Quality of Existing Code

Data Level Refactorings Replace a magic number with a named

constant Rename a variable with more

informative name Replace an expression with a method Introduce an intermediate variable Convert a multi-use variable to a

multiple single-use variables Convert a data primitive to a class Change an array to an object Encapsulate a collection

7

Page 8: Refactoring: Improving the Quality of Existing Code

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

Page 9: Refactoring: Improving the Quality of Existing Code

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

Page 10: Refactoring: Improving the Quality of Existing Code

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

10

Page 11: Refactoring: Improving the Quality of Existing Code

Class Interface Refactorings

Move a method to another class Convert one class to two Delete a class Remove the man in the middle Hide a delegating class Introduce (use) an extension class Encapsulate an exposed member

variable

11

Page 12: Refactoring: Improving the Quality of Existing Code

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

Page 13: Refactoring: Improving the Quality of Existing Code

Refactoring Tips Save the code you start with Make sure you have tests to assure the behavior after the code is refactored

Do refactorings one at a time Keep refactorings small Check-in and test often Don’t underestimate small changes

13

Page 14: Refactoring: Improving the Quality of Existing Code

Refactoring tools in practice

14

Demo

Page 15: Refactoring: Improving the Quality of Existing Code

Code Refactoring

Questions? ??

? ? ??

??

?http://academy.telerik.com