Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 20
1
Sharif University of Technology
^ The observable behavior of the software should not bechanged.
2
Sharif University of Technology
^ Why Should You Refactor?^
Refactoring Makes Software Easier to Understand Refactoring Helps You Find Bugs Refactoring Helps You Program Faster
3
Sharif University of Technology
^ When Should You Refactor? ^ When
Should You Refactor?
^ Refactor the third time you do something similar (The Rule ^ Refactor
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
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.
9.^ Primitive Obsession:
Excessive use of primitives, due to reluctance to
p^
,
use small objects for small tasks. 10. Switch Statements11. Parallel Inheritance Hierarchies:
Where every time you make a
subclass of one class, you also have to make a subclass of another. 12. Lazy Class:
A class that isn't doing enough to justify its maintenance. 13 Speculative Generality:
Classes and features have been added just
13.^ Speculative Generality:
Classes
and features have been added just
because a need for them may arise someday. 14 Temporary Field:
An instance variable that is set only in certain
14.^ Temporary Field:
An^ instance variable that is set only in certain
circumstances. 15. Message Chains:
Transitive visibility chains.
6
Sharif University of Technology
15.^ Message
Chains:
Transitive
visibility chains.
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
8
Sharif University of Technology
^ Extract Method^ ^
You have a code fragment that can be grouped together You^ have a code fragment that can be grouped together. Turn the fragment into a method whose name explains the purpose of themethod.
9
Sharif University of Technology
^ 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
^ 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
Mo e Method Move Method^ ^ A method is, or will be, using or used by more features of another class than theclass on which it is defined.^ ^ Create a new method with a similar body in the class it uses most. Either turnthe old method into a simple delegation, or remove it altogether. Department of Computer Engineering
12
Sharif University of Technology
^ 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
^ 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
^ Inline Class^ ^
A class isn't doing very much. Move all its features into another class and delete it Move all its features into another class and delete it.
15
Sharif University of Technology
^ 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:
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:
g^
j
^
A server class you are using needs an additional method, but you can'tmodify the classmodify the class. Create a method in the client class with an instance of the server classas its first argument.
g
^
A server class you are using needs several additional methods, but youcan't modify the class.C^ t
l^ th t
t i
th^
t^
th d^
M k^
thi
^ Create a new class that contains these extra methods. Make thisextension class a subclass or a wrapper of the original.Department of Computer Engineering
18
Sharif University of Technology
19
Sharif University of Technology
20
Sharif University of Technology