Refactoring Patterns in Software Engineering: Lecture 10 by Raman Ramsin, Slides of Software Engineering

A series of lecture notes from a university course on software engineering at sharif university of technology. The notes cover the topic of refactoring patterns, including definitions, benefits, and symptoms of bad code. The document also introduces various refactoring patterns and categories, such as composing methods, moving features between objects, and dealing with generalization. Students will find this document useful for understanding the concepts of refactoring and how to improve the design of existing code.

Typology: Slides

2011/2012

Uploaded on 02/19/2012

hester
hester 🇮🇷

4.5

(13)

84 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Patterns in
Software Engineering
Lecturer: Raman Ramsin
Lecture 10
Refactoring Patterns
Part 1
Department of Computer Engineering 1Sharif University of Technology
Part
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Refactoring Patterns in Software Engineering: Lecture 10 by Raman Ramsin and more Slides Software Engineering in PDF only on Docsity!

Patterns in Software Engineering Lecturer: Raman Ramsin^ Lecture 10^ Refactoring Patterns

Part 1

1

Sharif University of Technology

Part^

Patterns in Software Engineering – Lecture 10 Refactoring: DefinitionRefactoring:

Definition

„^ Refactoring:^

A change made to the internal structure of software tomake it^ „^ easier to understand, and^ „^ cheaper to modify.

^ The observable behavior of the software should not bechanged.

2

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Refactoring: When?Refactoring:

When?

„^ When Should You Refactor? „^ When

Should You Refactor?

^ Refactor the third time you do something similar (The Rule ^ Refactor

the third time you do something similar (The Rule
of Three)

Refactor When You Add Function Refactor When You Need to Fix a Bug Refactor As You Do a Code Review

4

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Symptoms of Bad Code (1)Symptoms

of Bad Code (1)

1.^ Duplicated Code2.^ Long Method3.^ Large Class

g

4.^ Long Parameter ListDi

t Ch^

Wh^

l^ i^

l^ h^

d i^ diff

t

5.^ Divergent Change:

When one class is commonly changed in different

ways for different reasons. 6 Shotgun Surgery:

When every time you make a kind of change

you

6.^ Shotgun Surgery:

When

every time you make a kind of change, you

have to make a lot of little changes to a lot of different classes. 7 Feature Envy:

A method that seems more interested in a class other than

7.^ Feature Envy:

A^ method that seems more interested in a class other than the one it actually is in. 8 Data Clumps:

Bunches of data that regularly appear together

5

Sharif University of Technology

8.^ Data Clumps:

Bunches

of data that regularly appear together.

Patterns in Software Engineering – Lecture 10 Symptoms of Bad Code (3)Symptoms

of Bad Code (3)

16 Middle Man:

Excessive delegation

16.^ Middle Man:

Excessive

delegation.

17.^ Inappropriate Intimacy:

Excessive interaction and coupling.

18.^ Alternative Classes with Different Interfaces:

Classes that do the

same thing but have different interfaces for what they do. 19. Incomplete Library Class20. Data Class:

Classes that have fields, getting and setting methods for the fields, and nothing else. 21. Refused Bequest:

When subclasses do not fulfill the commitments of

h^

l their superclasses. 22. Comments:

When comments are used to compensate for bad code.

7

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Refactoring Patterns: CategoriesRefactoring

Patterns: Categories

„^ Composing Methods:
Packaging code properly
p^
g^
g^ g^
p^ p^
y
„^ Moving Features Between Objects:
Reassigning responsibilities
O^
i i^
D t^
M ki^
d t^
i^ t^
k^ ith
„^ Organizing Data:
Making data easier to work with
„^ Simplifying Conditional Expressions:
Making conditional logic
less e
o^ p one
less error-prone „ Making Method Calls Simpler:
Making interfaces easy to
understand and useunderstand and use „ Dealing with Generalization:
Moving features around a hierarchy
of inheritanceof inheritance „ Big Refactorings:
Large-scale changes to code

8

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Composing Methods:

Inline Method

Composing

Methods:

Inline
Method

„^ Inline Method^ ^

A method

's body is just as clear as its name

^ A method s body is just as clear as its name. ^ Put the method's body into the body of its callers and remove the method. Department of Computer Engineering

10

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Composing Methods:

Replace Method with Method Object

Composing

Methods:

Replace
Method with Method Object

„^ Replace Method with Method Object^ ^

You have a long method that uses local variables in such a way that you cannot You^ have a long method that uses local variables in such a way that you cannotapplyExtract Method. Turn the method into an object so that all the local variables become fields onthat object

It can then be decomposed into other methods on the same object that object. It can then be decomposed into other methods on the same object.

11

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Moving Features Between Objects:

Move Field

Moving

Features Between Objects:

Move
Field

„^ Move Field^ ^

A field is, or will be, used by another class more than the class on which it isdefined. Create a new field in the target class, and change all its users.

13

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Moving Features Between Objects:

Extract Class

Moving

Features Between Objects:

Extract
Class

„^ Extract Class^ ^

You have one class doing work that should be done by two. Create a new class and move the relevant fields and methods from the old class Create a new class and move the relevant fields and methods from the old classinto the new class.

14

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Moving Features Between Objects:

Hide Delegate

Moving

Features Between Objects:

Hide
Delegate

„^ Hide Delegate

g

^ A client is calling a delegate class of an object. ^ Create methods on the server to hide the delegate. Department of Computer Engineering

16

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Moving Features Between Objects:

Remove Middle Man

g^

j

„^ Remove Middle Man^ ^

A class is doing too much simple delegation. Get the client to call the delegate directly.

17

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 Moving Features Between Objects:
Introduce Local Extension
g^
j

19

Sharif University of Technology

Patterns in Software Engineering – Lecture 10 R fReference „ Fowler, M.,

Refactoring: Improving the Design of Existing Code,

Addison-Wesley, 1999.Department of Computer Engineering

20

Sharif University of Technology