Refactoring

64
Refactoring Ricardo Terra rterrabh [at] gmail.com Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 1 / 46

Transcript of Refactoring

Page 1: Refactoring

Refactoring

Ricardo Terra

rterrabh [at] gmail.com

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 1 / 46

Page 2: Refactoring

CV

Nome: Ricardo Terra

Email: rterrabh [at] gmail.com

www: ricardoterra.com.br

Twitter: rterrabh

Lattes: lattes.cnpq.br/ 0162081093970868

Ph.D. (UFMG/UWaterloo),Post-Ph.D. (INRIA/Université Lille 1)

BackgroundEducation: UFLA (since 2014), UFSJ (1 year), FUMEC (3 years), UNIPAC (1 year), FAMINAS (3 years)

Work Experience: DBA Eng. (1 year), Synos (2 years), Stefanini (1 year)

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 2 / 46

Page 3: Refactoring

Agenda

1 Overview

2 Refactoring

3 Refactoring & Agile Methodologies

4 Final Considerations

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 3 / 46

Page 4: Refactoring

Overview

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 4 / 46

Page 5: Refactoring

Overview – What is it?

Process of changing a computer program’s source codewithout modifying its external functional behavior

To improve some of the nonfunctional attributes:

code readability

reduced complexity to improve maintainability

better design =⇒ extensibility

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 5 / 46

Page 6: Refactoring

Overview – What is it?

“By continuously improving the design of code, we makeit easier and easier to work with. This is in sharp contrastto what typically happens: little refactoring and a greatdeal of attention paid to expediently adding new features. Ifyou get into the hygienic habit of refactoring continuously,you’ll find that it is easier to extend and maintain code.”

— Joshua Kerievsky [Refactoring to Patterns]

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 6 / 46

Page 7: Refactoring

Overview – What is it?

“By continuously improving the design of code, we makeit easier and easier to work with. This is in sharp contrastto what typically happens: little refactoring and a greatdeal of attention paid to expediently adding new features. Ifyou get into the hygienic habit of refactoring continuously,you’ll find that it is easier to extend and maintain code.”

— Joshua Kerievsky [Refactoring to Patterns]

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 6 / 46

Page 8: Refactoring

Overview – What is it?

“By continuously improving the design of code, we makeit easier and easier to work with. This is in sharp contrastto what typically happens: little refactoring and a greatdeal of attention paid to expediently adding new features. Ifyou get into the hygienic habit of refactoring continuously,you’ll find that it is easier to extend and maintain code.”

— Joshua Kerievsky [Refactoring to Patterns]

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 6 / 46

Page 9: Refactoring

Overview – History of Use

In the past, refactoring was not a common practice indevelopment processes

For example, CVS (created in 1984) does not version themoving or renaming of files and directories

Nowadays, refactoring is one fundamental techniqueadopted in agile methodologies1

eXtreme Programming, Scrum, etc.

1I’ll discuss about it later...

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 7 / 46

Page 10: Refactoring

Overview – When do I use?

Bad smells!A structure in the code suggests, and sometimes evenscream for, opportunities for refactoring

This humorous advice rely on the experience of programmersand on the clarity of code

Indications to possible bad smells:Duplicated code →

share one same method

Long method →

extract one or more smaller methods

Large class →

divide in more cohesive classes

Long parameter list →

encapsulate them

...

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 8 / 46

Page 11: Refactoring

Overview – When do I use?

Bad smells!A structure in the code suggests, and sometimes evenscream for, opportunities for refactoring

This humorous advice rely on the experience of programmersand on the clarity of code

Indications to possible bad smells:Duplicated code → share one same method

Long method →

extract one or more smaller methods

Large class →

divide in more cohesive classes

Long parameter list →

encapsulate them

...

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 8 / 46

Page 12: Refactoring

Overview – When do I use?

Bad smells!A structure in the code suggests, and sometimes evenscream for, opportunities for refactoring

This humorous advice rely on the experience of programmersand on the clarity of code

Indications to possible bad smells:Duplicated code → share one same method

Long method → extract one or more smaller methods

Large class →

divide in more cohesive classes

Long parameter list →

encapsulate them

...

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 8 / 46

Page 13: Refactoring

Overview – When do I use?

Bad smells!A structure in the code suggests, and sometimes evenscream for, opportunities for refactoring

This humorous advice rely on the experience of programmersand on the clarity of code

Indications to possible bad smells:Duplicated code → share one same method

Long method → extract one or more smaller methods

Large class → divide in more cohesive classes

Long parameter list →

encapsulate them

...

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 8 / 46

Page 14: Refactoring

Overview – When do I use?

Bad smells!A structure in the code suggests, and sometimes evenscream for, opportunities for refactoring

This humorous advice rely on the experience of programmersand on the clarity of code

Indications to possible bad smells:Duplicated code → share one same method

Long method → extract one or more smaller methods

Large class → divide in more cohesive classes

Long parameter list → encapsulate them

...

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 8 / 46

Page 15: Refactoring

Refactoring

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 9 / 46

Page 16: Refactoring

Refactoring

There is a almost a hundred catalogued refactorings

each one has a name, a motivation, and a mechanics

They are usually organized into the following groups:

Composing Methods (e.g., Extract Method)

Moving Features Between Objects (e.g., Move Method)

Organizing Data (e.g., Encapsulate Field)

Simplifying Conditional Expressions (e.g., Consolidate Conditional Expression)

Making Method Calls Simpler (e.g., Rename Method)

Dealing with Generalization (e.g., Pull Up Method)

Big Refactorings (e.g., Extract Hierarchy)

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 10 / 46

Page 17: Refactoring

Refactoring

In this section, we will explain some refactorings and showan example of its usage

We have chosen the popular refactorings, more specifically,refactorings that Eclipse IDE has already automated:

Extract Method

Pull Up Method

Move Method

Extract Superclass

Introduce Parameter Object

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 11 / 46

Page 18: Refactoring

Extract Method

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 12 / 46

Page 19: Refactoring

Refactoring #1 – Extract Method

You have a code fragment that can be grouped together

⇓Turn the fragment into a method whose name explains the

purpose of the method

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 13 / 46

Page 20: Refactoring

Refactoring #1 – Extract Method – Theoretical Example

1 void printOwing (double amount) 2 printBanner ( ) ;3

4 // p r i n t deta i l s5 System . out . p r i n t l n ( "name: " + t h i s .name) ;6 System . out . p r i n t l n ( "amount : " + amount ) ;7

1 void printOwing (double amount) 2 printBanner ( ) ;3 p r i n t D e t a i l s (amount ) ;4 5

6 void p r i n t D e t a i l s (double amount) 7 System . out . p r i n t l n ( "name: " + t h i s .name) ;8 System . out . p r i n t l n ( "amount : " + amount ) ;9

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 14 / 46

Page 21: Refactoring

Refactoring #1 – Extract Method – Theoretical Example

1 void printOwing (double amount) 2 printBanner ( ) ;3

4 // p r i n t deta i l s5 System . out . p r i n t l n ( "name: " + t h i s .name) ;6 System . out . p r i n t l n ( "amount : " + amount ) ;7

1 void printOwing (double amount) 2 printBanner ( ) ;3 p r i n t D e t a i l s (amount ) ;4 5

6 void p r i n t D e t a i l s (double amount) 7 System . out . p r i n t l n ( "name: " + t h i s .name) ;8 System . out . p r i n t l n ( "amount : " + amount ) ;9

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 14 / 46

Page 22: Refactoring

Refactoring #1 – Extract Method – Theoretical Example

1 void printOwing (double amount) 2 printBanner ( ) ;3

4 // p r i n t deta i l s5 System . out . p r i n t l n ( "name: " + t h i s .name) ;6 System . out . p r i n t l n ( "amount : " + amount ) ;7

1 void printOwing (double amount) 2 printBanner ( ) ;3 p r i n t D e t a i l s (amount ) ;4 5

6 void p r i n t D e t a i l s (double amount) 7 System . out . p r i n t l n ( "name: " + t h i s .name) ;8 System . out . p r i n t l n ( "amount : " + amount ) ;9

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 14 / 46

Page 23: Refactoring

Refactoring #1 – Extract Method – Motivation

It is one of the most common refactorings

Long methods or look at code that needs a comment tounderstand its purpose

So, I turn that fragment of code into its own method.Reasons to do that:

Increases the chances that other methods can use a methodwhen the method is fine-grained

Allows the higher-level methods to read more like a series ofcomments

Overriding also is easier

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 15 / 46

Page 24: Refactoring

Refactoring #1 – Extract Method – Practice

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 16 / 46

Page 25: Refactoring

Pull Up Method

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 17 / 46

Page 26: Refactoring

Refactoring #2 – Pull Up Method

You have methods with identical results on subclasses

⇓Move them to the superclass

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 18 / 46

Page 27: Refactoring

Refactoring #2 – Pull Up Method – Theoretical Example

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 19 / 46

Page 28: Refactoring

Refactoring #2 – Pull Up Method – Theoretical Example

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 19 / 46

Page 29: Refactoring

Refactoring #2 – Pull Up Method – Theoretical Example

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 19 / 46

Page 30: Refactoring

Refactoring #2 – Pull Up Method – Motivation

Eliminating duplicate behavior is important

if not, the risk that a change to one not be made to other

The easiest case: methods have the same body

of course it is not always obvious as that. So, look for thedifferences and test for safety

Special case: you have a subclass method that overrides asuperclass method yet does the same thing (WDF?)

The most awkward case: the method may refer to featuresthat are on the subclass but not on the superclass

possible solutions: generalize methods, create abstractmethod on superclass, change a method’s signature...

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 20 / 46

Page 31: Refactoring

Refactoring #2 – Pull Up Method – Practice

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 21 / 46

Page 32: Refactoring

Move Method

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 22 / 46

Page 33: Refactoring

Refactoring #3 – Move Method

A method is, or will be, using or used by more features of anotherclass than the class on which it is defined

(Feature Envy)

⇓Create a new method with similar body in the class it uses most.Either turn the old method into a simple delegation, or remove it

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 23 / 46

Page 34: Refactoring

Refactoring #3 – Move Method – Theoretical Example

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 24 / 46

Page 35: Refactoring

Refactoring #3 – Move Method – Theoretical Example

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 24 / 46

Page 36: Refactoring

Refactoring #3 – Move Method – Motivation

Moving methods is the bread and butter of refactoring

classes with too much behavior

classes are collaborating too much (too highly coupled)

By moving methods around:

make classes simpler

high cohesion (classes end up being a more crisp implementation of a set of responsibilities)

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 25 / 46

Page 37: Refactoring

Refactoring #3 – Move Method – Practice

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 26 / 46

Page 38: Refactoring

Extract Superclass

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 27 / 46

Page 39: Refactoring

Refactoring #4 – Extract Superclass

You have two classes with similar features

⇓Create a superclass and move the common features to the

superclass

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 28 / 46

Page 40: Refactoring

Refactoring #4 – Extract Superclass – Theoretical Example

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 29 / 46

Page 41: Refactoring

Refactoring #4 – Extract Superclass – Theoretical Example

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 29 / 46

Page 42: Refactoring

Refactoring #4 – Extract Superclass – Theoretical Example

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 29 / 46

Page 43: Refactoring

Refactoring #4 – Extract Superclass – Theoretical Example

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 29 / 46

Page 44: Refactoring

Refactoring #4 – Extract Superclass – Motivation

Duplicate code is one of the principal bad things in systems

Duplicate code:

two classes that do similar things in the same way or

two classes that do similar things in different ways

A well-known solution: inheritance (everything)

However, you often do not notice the commonalities until youhave created some classes

In this case, you need to create the inheritance structure later

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 30 / 46

Page 45: Refactoring

Refactoring #4 – Extract Superclass – Practice

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 31 / 46

Page 46: Refactoring

Introduce Parameter Object

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 32 / 46

Page 47: Refactoring

Refactoring #5 – Introduce Parameter Object

You have a group of parameters that naturally go together

⇓Replace them with an object

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 33 / 46

Page 48: Refactoring

Refactoring #5 – Introduce Parameter Object – TheoreticalExample

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 34 / 46

Page 49: Refactoring

Refactoring #5 – Introduce Parameter Object – TheoreticalExample

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 34 / 46

Page 50: Refactoring

Refactoring #5 – Introduce Parameter Object – TheoreticalExample

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 34 / 46

Page 51: Refactoring

Refactoring #5 – Introduce Parameter Object – TheoreticalExample

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 34 / 46

Page 52: Refactoring

Refactoring #5 – Introduce Parameter Object – TheoreticalExample

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 34 / 46

Page 53: Refactoring

Refactoring #5 – Introduce Parameter Object – Motivation

Often you see a particular group of parameters that tend tobe passed together

It is worthwhile to turn these parameters into objects just togroup the data together

It is useful because it reduces the size of the parameter lists,and long parameter list are hard to understand

Deeper, when you do this refactoring, you can see commonmanipulations of the parameter values

By moving this behavior into the new object, you can removea lot of duplicated code

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 35 / 46

Page 54: Refactoring

Refactoring #5 – Introduce Parameter Object – Practice

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 36 / 46

rename as well?

Page 55: Refactoring

Refactoring & Agile Methodologies

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 37 / 46

Page 56: Refactoring

Refactoring & Agile Methodologies – XP

eXtreme Programming (XP)

XP prescribes several engineering practices:

TDD (automated testing), Pair programming, Simple design,

and Refactoring (mercilessly)

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 38 / 46

Page 57: Refactoring

Refactoring & Agile Methodologies – XP

eXtreme Programming (XP) — Collective Code Ownership

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 39 / 46

Page 58: Refactoring

Refactoring & Agile Methodologies – Scrum

Scrum

Scrum, on the other hand, does not prescribe engineeringpractices

But, it does not mean you can’t use TDD, PP, Refactoring, etc.

I.M.H.O, Scrum solely does not make their usage mandatory

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 40 / 46

Page 59: Refactoring

Final Considerations

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 41 / 46

Page 60: Refactoring

Final Considerations – Why should I refactor?

Improves the design of software

Loss of the structure (mostly by changes) has a cumulative effect

Regular refactorings helps code retain its shape

Makes software easier to understand

when you refactor, your code is more readable

Helps you find bugs

when you refactor, you understand what the code does, andput that understanding right back into the code

Helps you program faster (does it sound counterintuitive?)

without a good design, you spend time finding and fixingbugs instead of adding new functions

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 42 / 46

Page 61: Refactoring

Final Considerations – Refactoring vs Performance Optimization

Like refactoring, performance optimization does not changethe behavior of a component (other that its speed)

Indeed, it only alters the internal structure

However, the purpose is different

Refactoring: software easier to understand and modify

Performance Optimization: often makes code harder tounderstand and modify

but you get the performance you need

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 43 / 46

Page 62: Refactoring

Final Considerations – When should I refactor?

“Refactoring is something you do all the time in little bursts. Youdon’t decide to refactor, you refactor because you want to dosomething else, and refactoring helps you do that other thing.”

— Martin Fowler [Refactoring: Improving the Design of Existing Code]

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 44 / 46

Page 63: Refactoring

References

William Opdyke. Refactoring object-oriented frameworks.Ph.D. thesis, University of Illinois at Urbana-Champaign, 1992.

Martin Fowler et al. Refactoring: Improving the Design ofExisting Code. Addison Wesley, 1999.

Joshua Kerievsky. Refactoring to Patterns. Addison Wesley,2004.

Ken Schwaber and Jeff Sutherland. The Scrum Guide. 2011.Available at: http://www.scrum.org.

Don Wells. Extreme Programming: A gentle introduction.2011. Available at: http://www.extremeprogramming.org.

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 45 / 46

Page 64: Refactoring

Thanks a lot!!!

This presentation and the Eclipse Java project are publicly available at:www.ricardoterra.com.br/public

Ricardo Terra (rterrabh [at] gmail.com) Refactoring Janeiro, 2014 46 / 46