SlideShare una empresa de Scribd logo
1 de 57
Software Evolution and
Refactoring
July 2010
1
Introduction
• Any changes on software by developers or
users
• These changes are costly
• How we can design (re-design) and develop
software to reduce the cost of future
changes?
2
Relative distribution of
software/hardware costs
SE, Maintenance, Hans van Vliet, ©2008 3
Hardware
Development
Software
Maintenance
1955 1970 1985
Year
100
60
20
Percent
of
total
cost
Importance (Business Perspective)
• Organizations have huge investments in their
software systems - they are critical business
assets.
• To maintain the value of these assets to the
business, they must be changed and updated.
• The majority of the software budget in large
companies is devoted to evolving existing
software rather than developing new software.
• Studies indicate that up to 75% of all software
professionals are involved in some form of
maintenance/evolution activity.
4
Importance (Engineering Perspective)
• Developing new software is costly and time
consuming
– It is insane to build a new system for every
required change in your software
• Reuse your software in another system
• Add some new features
• Fix some detected bugs
• …
5
Reasons for Software Change
• Corrective: Repair software faults
– Changing a system to correct deficiencies in the way meets its
requirements.
• Adaptive: Adapt software to a different operating environment
– Change for reuse in another system
– Changing a system so that it operates in a different environment
(computer, OS, etc.) from its initial implementation.
• Perfective: Add to or modify the system’s functionality
– Modifying the system to satisfy new requirements.
– Performance tuning
• Preventive: Improve the program structure
– Rewriting all or parts of the system to make it more efficient and
maintainable.
– Restructure code, “refactoring”, legacy wrapping, build interfaces
6
Distribution of maintenance activities
SE, Maintenance, Hans van Vliet, ©2008 7
corrective 21%
adaptive 25%
preventive 4%
perfective 50%
How to Reduce Maintenance Changes
• Higher quality  less (corrective) maintenance
• Anticipating changes  less (adaptive and perfective)
maintenance
• Better tuning to user needs  less (perfective)
maintenance
• Regularly perform preventive maintenance
• Less code  less maintenance
SE, Maintenance, Hans van Vliet, ©2008 8
Lemman’s Laws of Software Evolution
• Total 8 Laws
– Law 1: Software Change is inevitable
– Law 2: As software changes, it becomes more
complex
– …
9
Lemman’s Laws in a Nutshell
• Observations:
– Code Decay: (Most) useful software must evolve or die.
– Code Ageing: As a software system gets bigger, its resulting
complexity tends to limit its ability to grow.
• Advice:
– Need to manage complexity.
– Do periodic redesigns, and refinements.
– Treat software and its development process as a feedback
system.
10
Terminology
• Evolution
• Maintenance
• Re-engineering
• Reverse Engineering
• Program Understanding
• Program Comprehension
• Forward Engineering
• Program Transformation
• Restructuring
• Design Recovery
• Modernization
• Retrofitting
• Renovation
• Migration
• Refactoring
• Refinement
• Replacement
• Redocumentation
• … and many more
11
Maintenance Vs. Evolution
• In many cases both terms are used interchangeably, or
together.
• Some researchers consider evolution as a subset of
maintenance, while some others define them vise
versa
• If you are Confused, as a rule of thumb:
– Software maintenance addresses bug fixes and minor
enhancements (corrective, and preventive).
– software evolution focuses on more extensive changes
(perfective, and adaptive).
12
Engineering Vs. Reengineering
• Engineering is a process of designing and
developing a new product.
• Reengineering is a process of understanding
and changing an existing product for various
reasons.
– Usually, you reengineer a system that you did not
developed initially.
13
Let’s Visualize it!
SE, Maintenance, Hans van Vliet, ©2008 14
What Makes software hard to
Maintain?
• Unstructured and complex code
– Low Software Quality
– Result of poor design and not performing preventive
maintenance
• Insufficient domain knowledge
– Predict future demands, change requests, and …
• Insufficient and out of sync documentation
– How to update design documents?
– Naming Conversions
– Commenting
SE, Maintenance, Hans van Vliet, ©2008 15
Why do we care?!
• What should we learn in this (Architecture and
Design) course to reduce maintenance
problems?
– Quality design
– Common solutions are easier to maintain (Use
design patterns and architectural styles)
– Use CASE tools and common notations to keep
the design in sync with code
– Redesign (Preventive) to improve quality and
remove bad code smells!
16
Bad Smells in Code
• You code is decaying… It is getting old and
ugly… It stinks…
• The most common design problems result
from code that:
– Is duplicated
– Is unclear
– Is complicated
17
Some Code Smells
• Duplicate code
• Long method
• Conditional Complexity
• Data Class
• Solution Sprawl
• Switch statements
• Large class
• Lazy class
• Combinatorial Explosion
• Long Parameter List
• Shutgun Surgery
• Data Clumps
• Comments!
• And many more…
18
Five Groups of Bad Code Smells
• The Bloaters: represents something that has grown so large that it
cannot be effectively handled.
• The Object-Orientation Abusers: they represent cases where the
solution does not fully exploit the possibilities of object-oriented
design.
• The Change Preventers: are smells that hinder changing or further
developing the software.
• The Dispensables: represent something unnecessary that should be
removed from the source code.
• The Couplers: coupling-related smells.
19
Bad Smells are the Indicators of
System Decay
• Frequent failures
• Overly complex structure
• Running in emulation mode
• Very large components
• Excessive resource requirements
• Deficient documentation
• High personnel turnover
• Different technologies in one system
20
How to remove bad smells?
• Via a reengineering process, three steps:
– Understanding
– Transforming
– Refining
• But we don’t want to change the program’s
behavior!
• So we need a set of transformations that are
guaranteed to preserve the behavior while they
can remove bad smells
– We call them “Refactorings”
21
22
Refactoring
• Refactoring is:
– restructuring (rearranging) code...
– ...in a series of small, semantics-preserving transformations (i.e. the
code keeps working)...
– ...in order to make the code easier to maintain and modify
• Refactoring is not just any old restructuring
– You need to keep the code working
– You need small steps that preserve semantics
– You need to have unit tests to prove the code works
• There are numerous well-known refactoring techniques
– You should be at least somewhat familiar with these before inventing
your own
Major Refactoring Catalogs
• Refactoring: Improving the Design of Existing
Code by Martin Fowler with contributions by
Kent Beck, John Brant, William Opdyke, and
Don Roberts, Addison-Wesley 1999.
• Refactoring to Patterns by Joshua Kerievsky,
Addison-Wesley 2004.
23
Primitive (Basic) Refactorings
• Behavior-preserving transformations:
– Move Method
– Rename Method
– Add Class
– Extract Method
– Pull up Method
– …
• Can be used as building blocks to create the so-called
composite refactorings.
– Example:
• MoveMethodsToVisitor
• Add Template Method
24
See:
www.refactoring.com
25
When Should We Refactor
• You should refactor:
– Any time that you see a better way to do things
• “Better” means making the code easier to understand and to
modify in the future
– You can do so without breaking the code
• Unit tests are essential for this
– You smell it! (If it stinks, change it)
• You should not refactor:
– Stable code (code that won’t ever need to change)
– Someone else’s code
• Unless you’ve inherited it (and now it’s yours)
What to refactor
Model (High-level)
• Not all code smells are design
smells!
• We need reverse engineering
• More abstract, less detail
• Can be visualized
• Can be hard to reflect the
changes to the code
(refinement)
• Can be automated (more
risky)
Code (Low-level)
• Hard to Understand
• Highly Detailed
• We can perform Unit testing
after refactorings
• Can be automated
26
27
Example 1: switch statements
• switch statements are very rare in properly
designed object-oriented code
– Therefore, a switch statement is a simple and easily
detected “bad smell”
– Of course, not all uses of switch are bad
– A switch statement should not be used to distinguish
between various kinds of object
• There are several well-defined refactorings for
this case
– The simplest is the creation of subclasses
28
Example 1, continued
• class Animal {
final int MAMMAL = 0, BIRD = 1, REPTILE = 2;
int myKind; // set in constructor
...
String getSkin() {
switch (myKind) {
case MAMMAL: return "hair";
case BIRD: return "feathers";
case REPTILE: return "scales";
default: return "integument";
}
}
}
29
Example 1, improved
• class Animal {
String getSkin() { return "integument"; }
}
class Mammal extends Animal {
String getSkin() { return "hair"; }
}
class Bird extends Animal {
String getSkin() { return "feathers"; }
}
class Reptile extends Animal {
String getSkin() { return "scales"; }
}
30
How is this an improvement?
• Adding a new animal type, such as Insect, does
not require revising and recompiling existing code
• Mammals, birds, and reptiles are likely to differ in
other ways, and we’ve already separated them
out (so we won’t need more switch statements)
• We’ve gotten rid of the flags we needed to tell
one kind of animal from another
• Basically, we’re now using Objects the way they
were meant to be used
31
JUnit tests
• As we refactor, we need to run JUnit tests to ensure that we
haven’t introduced errors
• public void testGetSkin() {
assertEquals("hair", myMammal.getSkin());
assertEquals("feathers", myBird.getSkin());
assertEquals("scales", myReptile.getSkin());
assertEquals("integument", myAnimal.getSkin());
}
• This should work equally well with either implementation
Refactoring to Patterns
• Argue that design patterns can remove code
smells
• More high level
• Each refactoring to patterns is composed of a set
of Primitive refactorings
• Don’t Forget Testing after refactoring!
• 21 refactorings are introduced in:
– Refactoring to Patterns by Joshua Kerievsky, Addison-
Wesley 2004.
32
33
Catalog of Refactorings to Patterns
1. Replace Constructors with Creation methods
2. Encapsulate Classes with Factory
3. Introduce Polymorphic Creation with Factory Method
4. Replace Conditional Logic with Strategy
5. Form Template Method
6. Compose Method
7. Replace Implicit Tree with Composite
8. Encapsulate Composite with Builder
9. Move Accumulation to Collecting Parameter
10. Extract Composite, Replace one/many with Composite.
11. Replace Conditional Dispatcher with Command
12. Extract Adapter, Unify Interfaces with Adapter
13. Replace Type Code with Class
34
Catalog of Refactorings to Patterns
14. Replace State-Altering Conditionals with State
15. Introduce Null Object
16. Inline Singleton, Limit Instantiation with Singleton
17. Replace Hard-Coded Notifications with Observer
18. Move Embellishment to Decorator, Unify Interfaces, Extract Parameter
19. Move Creation Knowledge to Factory
20. Move Accumulation to Visitor
21. Replace Implicit Language with Interpreter
Are these patterns themselves?
35
Each refactoring has
• Name and Intent
• Gives an application example
• Discusses motivation
• Benefits and Liabilities
• Mechanics
– Specific things to do
• Presents detailed example
How to Use Refactorings
• Locate your Code/Design bad smells
• Look up for the list of refactorings to resolve
that smell.
• Example: Conditional Complexity
– Replace conditional logic with Strategy
– Move embellishment to Decorator
– Replace state-altering conditionals with State
– (Introduce Null Object)
36
How to use the refactorings
• Example: Duplicated Code
– Form Template Method
– Introduce polymorphic creation with Factory
Method
– (Chain Constructors)
– Extract composite
– Unify interfaces with Adapter
37
Example 2: Duplicated code
• If the same code fragment occurs in more than one
place within a single class, you can use Extract Method
– Turn the fragment into a method whose name explains the
purpose of the method
– Any local variables that the method requires can be passed
as parameters (if there aren’t too many of them!)
– If the method changes a local variable, see whether it
makes sense to return that as the value of the method
(possibly changing the name of the method to indicate
this)
– If the method changes two or more variables, you need
other refactorings to fix this problem
Example 2: How to remove Duplicate
Code
• If the same code fragment occurs in sibling classes, you
can use Extract Method in both classes, then use Pull
Up Method
– Use ExtractMethod in each class
– Be sure the code is identical
– If necessary, adjust the method signatures to be identical
– Copy the extracted method to the common superclass
– Delete one subclass method
– Compile and test
– Delete the other subclass method
– Compile and test
Example 2: How to remove Duplicate
Code II
• If the same code fragment occurs in unrelated
classes, you can use Extract Method in one class,
then:
– Use this class as a component in the other class, or
– Invoke the method from the other class, or
– Move the method to a third class and refer to it from
both of the original classes
• In any case, you need to decide where the
method makes most sense, and put it there
Example 2: A Hint
• If almost the same code fragment occurs in
sibling classes, use Extract Method to separate
the similar bits from the different bits, and use
Form Template Method
Intent of Template Method
• Define the skeleton of an algorithm in an
operation, deferring some steps to subclasses.
• Template Method lets subclasses redefine
certain steps of an algorithm without changing
the algorithm’s structure.
42
The Template Method
• Template Methods lead to an inverted control
structure
– A superclass calls methods in its subclass
• Template methods are so fundamental that
they can be found in almost every abstract
class
• Template Method uses inheritance
• A similar pattern, Strategy Pattern, uses
delegation rather than inheritance
Example 2: Big fish and little fish
• The scenario: “big fish” and “little fish” move
around in an “ocean”
– Fish move about randomly
– A big fish can move to where a little fish is (and
eat it)
– A little fish will not move to where a big fish is
BigFish
move()
Fish
<<abstract>>move()
LittleFish
move()
Example 2: The move() method
• General outline of the method:
– public void move() {
choose a random direction; // same for both
find the location in that direction; // same for both
check if it’s ok to move there; // different
if it’s ok, make the move; // same for both
}
• Solution:
– Extract the check on whether it’s ok to move
– In the Fish class, put the actual (template) move()
method
– Create an abstract okToMove() method in the Fish class
– Implement okToMove() in each subclass
The Fish refactoring
BigFish
move()
Fish
<<abstract>>move()
LittleFish
move()
BigFish
okToMove(locn):boolean
Fish
move()
<<abstract>>okToMove(locn):boolean
BigFish
okToMove(locn):boolean
• Note how this works:
When a BigFish tries
to move, it uses the
move() method in
Fish
• But the move()
method in Fish uses
the okToMove(locn)
method in BigFish
• And similarly for
LittleFish
Example 3: Replace Constructor with
Creation (Factory) Methods
• Problem: Constructors on a class make it hard
to decide which constructor to call during
development.
• Solution: Replace the constructors with
intention-revealing Creation (Factory)
Methods that return object instances .
47
Example 3: Class Diagram
48
Example 3: Mechanics (Step I)
• Find a client that calls a class’s constructor in
order to create a kind of instance.
• Apply Extract Method on the constructor call
to produce a public, static method.
• This new method is a creation method.
• Apply Move Method to move the creation
method to the class containing the chosen
constructor.
• Compile and test.
49
Example 3: Mechanics (Step II)
• Find all callers of the chosen constructor that
instantiate the same kind of instance as the
creation method.
• Update them to call the creation method.
• Compile and test.
50
Example 3: Mechanics (Step III)
• If the chosen constructor is chained to
another constructor:
– Make the creation method call the chained
constructor instead of the chosen constructor.
• Inline the constructor (Apply Inline method).
• Compile and Test.
51
Example 3: Mechanics (Final Step)
• Repeat steps 1-3 for every constructor on the
class that you’d like to turn into Creation
Method.
• If a constructor on the class has no callers
outside the class, make it non-public.
• Compile and test.
52
53
54
55
Example 3: Benefits and Liabilities
• Communicates what kinds of instances are
available better than constructors.
• Bypasses constructor limitations, such as the
inability to have two constructors with the same
number and type of arguments.
• Makes it easier to find unused creation code.
• - Makes creation nonstandard: some classes are
instantiated using new, while others use Creation
methods.
56
Tool Support
• There are several CASE tools to automate the
Code and Design Refactoring.
– Eclipse: has a build in refactoring that supports a
set of primitive refactorings.
– RefactorIT: supports a large set of refactorings.
Also detects some code smells by computing
design metrics. Eclipse plugin.
– Borland Together: Famous for its design level
refactorings to patterns, but expensive!
– Some tools exist for VisualStudio, too.
57

Más contenido relacionado

Similar a Software Evolution and Refactoring Techniques

Software Engineering Lec 1-introduction
Software Engineering Lec 1-introductionSoftware Engineering Lec 1-introduction
Software Engineering Lec 1-introductionTaymoor Nazmy
 
Lecture 3 software_engineering
Lecture 3 software_engineeringLecture 3 software_engineering
Lecture 3 software_engineeringmoduledesign
 
Continuous Delivery Decision points
Continuous Delivery Decision pointsContinuous Delivery Decision points
Continuous Delivery Decision pointsKelly Looney
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentationBhavin Gandhi
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)Peter Kofler
 
An Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationAn Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationBlue Elephant Consulting
 
Software Engineering- Crisis and Process Models
Software Engineering- Crisis and Process ModelsSoftware Engineering- Crisis and Process Models
Software Engineering- Crisis and Process ModelsNishu Rastogi
 
01- Lecture -Introduction to IT Agile Development.
01- Lecture -Introduction to IT Agile Development.01- Lecture -Introduction to IT Agile Development.
01- Lecture -Introduction to IT Agile Development.mailtoahmedhassan
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practicesBill Buchan
 
Lecture 3 software_engineering
Lecture 3 software_engineeringLecture 3 software_engineering
Lecture 3 software_engineeringmoduledesign
 
Devops - why, what and how?
Devops - why, what and how?Devops - why, what and how?
Devops - why, what and how?Malinda Kapuruge
 
When agility meets software quality
When agility meets software qualityWhen agility meets software quality
When agility meets software qualityBabak Khorrami
 
Gartner Infrastructure and Operations Summit Berlin 2015 - DevOps Journey
Gartner Infrastructure and Operations Summit Berlin 2015 - DevOps JourneyGartner Infrastructure and Operations Summit Berlin 2015 - DevOps Journey
Gartner Infrastructure and Operations Summit Berlin 2015 - DevOps JourneyKelly Looney
 
Software Product Lines by Dr. Indika Kumara
Software Product Lines by Dr. Indika KumaraSoftware Product Lines by Dr. Indika Kumara
Software Product Lines by Dr. Indika KumaraThejan Wijesinghe
 
Software Project management
Software Project managementSoftware Project management
Software Project managementsameer farooq
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tipsBill Buchan
 
Introduction to DevOps slides-converted (1).pptx
Introduction to DevOps slides-converted (1).pptxIntroduction to DevOps slides-converted (1).pptx
Introduction to DevOps slides-converted (1).pptxaasssss1
 
Software maintenance Unit5
Software maintenance  Unit5Software maintenance  Unit5
Software maintenance Unit5Mohammad Faizan
 

Similar a Software Evolution and Refactoring Techniques (20)

Software Engineering Lec 1-introduction
Software Engineering Lec 1-introductionSoftware Engineering Lec 1-introduction
Software Engineering Lec 1-introduction
 
Lecture 3 software_engineering
Lecture 3 software_engineeringLecture 3 software_engineering
Lecture 3 software_engineering
 
Continuous Delivery Decision points
Continuous Delivery Decision pointsContinuous Delivery Decision points
Continuous Delivery Decision points
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
 
An Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationAn Introduction To Software Development - Implementation
An Introduction To Software Development - Implementation
 
Software Engineering- Crisis and Process Models
Software Engineering- Crisis and Process ModelsSoftware Engineering- Crisis and Process Models
Software Engineering- Crisis and Process Models
 
01- Lecture -Introduction to IT Agile Development.
01- Lecture -Introduction to IT Agile Development.01- Lecture -Introduction to IT Agile Development.
01- Lecture -Introduction to IT Agile Development.
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practices
 
Lecture 3 software_engineering
Lecture 3 software_engineeringLecture 3 software_engineering
Lecture 3 software_engineering
 
Devops - why, what and how?
Devops - why, what and how?Devops - why, what and how?
Devops - why, what and how?
 
When agility meets software quality
When agility meets software qualityWhen agility meets software quality
When agility meets software quality
 
Gartner Infrastructure and Operations Summit Berlin 2015 - DevOps Journey
Gartner Infrastructure and Operations Summit Berlin 2015 - DevOps JourneyGartner Infrastructure and Operations Summit Berlin 2015 - DevOps Journey
Gartner Infrastructure and Operations Summit Berlin 2015 - DevOps Journey
 
Manual Software testing - software development life cycle
Manual Software testing - software development life cycleManual Software testing - software development life cycle
Manual Software testing - software development life cycle
 
Software Product Lines by Dr. Indika Kumara
Software Product Lines by Dr. Indika KumaraSoftware Product Lines by Dr. Indika Kumara
Software Product Lines by Dr. Indika Kumara
 
Software Project management
Software Project managementSoftware Project management
Software Project management
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tips
 
Introduction to DevOps slides-converted (1).pptx
Introduction to DevOps slides-converted (1).pptxIntroduction to DevOps slides-converted (1).pptx
Introduction to DevOps slides-converted (1).pptx
 
Unit5.pptx
Unit5.pptxUnit5.pptx
Unit5.pptx
 
Software maintenance Unit5
Software maintenance  Unit5Software maintenance  Unit5
Software maintenance Unit5
 

Último

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Último (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Software Evolution and Refactoring Techniques

  • 2. Introduction • Any changes on software by developers or users • These changes are costly • How we can design (re-design) and develop software to reduce the cost of future changes? 2
  • 3. Relative distribution of software/hardware costs SE, Maintenance, Hans van Vliet, ©2008 3 Hardware Development Software Maintenance 1955 1970 1985 Year 100 60 20 Percent of total cost
  • 4. Importance (Business Perspective) • Organizations have huge investments in their software systems - they are critical business assets. • To maintain the value of these assets to the business, they must be changed and updated. • The majority of the software budget in large companies is devoted to evolving existing software rather than developing new software. • Studies indicate that up to 75% of all software professionals are involved in some form of maintenance/evolution activity. 4
  • 5. Importance (Engineering Perspective) • Developing new software is costly and time consuming – It is insane to build a new system for every required change in your software • Reuse your software in another system • Add some new features • Fix some detected bugs • … 5
  • 6. Reasons for Software Change • Corrective: Repair software faults – Changing a system to correct deficiencies in the way meets its requirements. • Adaptive: Adapt software to a different operating environment – Change for reuse in another system – Changing a system so that it operates in a different environment (computer, OS, etc.) from its initial implementation. • Perfective: Add to or modify the system’s functionality – Modifying the system to satisfy new requirements. – Performance tuning • Preventive: Improve the program structure – Rewriting all or parts of the system to make it more efficient and maintainable. – Restructure code, “refactoring”, legacy wrapping, build interfaces 6
  • 7. Distribution of maintenance activities SE, Maintenance, Hans van Vliet, ©2008 7 corrective 21% adaptive 25% preventive 4% perfective 50%
  • 8. How to Reduce Maintenance Changes • Higher quality  less (corrective) maintenance • Anticipating changes  less (adaptive and perfective) maintenance • Better tuning to user needs  less (perfective) maintenance • Regularly perform preventive maintenance • Less code  less maintenance SE, Maintenance, Hans van Vliet, ©2008 8
  • 9. Lemman’s Laws of Software Evolution • Total 8 Laws – Law 1: Software Change is inevitable – Law 2: As software changes, it becomes more complex – … 9
  • 10. Lemman’s Laws in a Nutshell • Observations: – Code Decay: (Most) useful software must evolve or die. – Code Ageing: As a software system gets bigger, its resulting complexity tends to limit its ability to grow. • Advice: – Need to manage complexity. – Do periodic redesigns, and refinements. – Treat software and its development process as a feedback system. 10
  • 11. Terminology • Evolution • Maintenance • Re-engineering • Reverse Engineering • Program Understanding • Program Comprehension • Forward Engineering • Program Transformation • Restructuring • Design Recovery • Modernization • Retrofitting • Renovation • Migration • Refactoring • Refinement • Replacement • Redocumentation • … and many more 11
  • 12. Maintenance Vs. Evolution • In many cases both terms are used interchangeably, or together. • Some researchers consider evolution as a subset of maintenance, while some others define them vise versa • If you are Confused, as a rule of thumb: – Software maintenance addresses bug fixes and minor enhancements (corrective, and preventive). – software evolution focuses on more extensive changes (perfective, and adaptive). 12
  • 13. Engineering Vs. Reengineering • Engineering is a process of designing and developing a new product. • Reengineering is a process of understanding and changing an existing product for various reasons. – Usually, you reengineer a system that you did not developed initially. 13
  • 14. Let’s Visualize it! SE, Maintenance, Hans van Vliet, ©2008 14
  • 15. What Makes software hard to Maintain? • Unstructured and complex code – Low Software Quality – Result of poor design and not performing preventive maintenance • Insufficient domain knowledge – Predict future demands, change requests, and … • Insufficient and out of sync documentation – How to update design documents? – Naming Conversions – Commenting SE, Maintenance, Hans van Vliet, ©2008 15
  • 16. Why do we care?! • What should we learn in this (Architecture and Design) course to reduce maintenance problems? – Quality design – Common solutions are easier to maintain (Use design patterns and architectural styles) – Use CASE tools and common notations to keep the design in sync with code – Redesign (Preventive) to improve quality and remove bad code smells! 16
  • 17. Bad Smells in Code • You code is decaying… It is getting old and ugly… It stinks… • The most common design problems result from code that: – Is duplicated – Is unclear – Is complicated 17
  • 18. Some Code Smells • Duplicate code • Long method • Conditional Complexity • Data Class • Solution Sprawl • Switch statements • Large class • Lazy class • Combinatorial Explosion • Long Parameter List • Shutgun Surgery • Data Clumps • Comments! • And many more… 18
  • 19. Five Groups of Bad Code Smells • The Bloaters: represents something that has grown so large that it cannot be effectively handled. • The Object-Orientation Abusers: they represent cases where the solution does not fully exploit the possibilities of object-oriented design. • The Change Preventers: are smells that hinder changing or further developing the software. • The Dispensables: represent something unnecessary that should be removed from the source code. • The Couplers: coupling-related smells. 19
  • 20. Bad Smells are the Indicators of System Decay • Frequent failures • Overly complex structure • Running in emulation mode • Very large components • Excessive resource requirements • Deficient documentation • High personnel turnover • Different technologies in one system 20
  • 21. How to remove bad smells? • Via a reengineering process, three steps: – Understanding – Transforming – Refining • But we don’t want to change the program’s behavior! • So we need a set of transformations that are guaranteed to preserve the behavior while they can remove bad smells – We call them “Refactorings” 21
  • 22. 22 Refactoring • Refactoring is: – restructuring (rearranging) code... – ...in a series of small, semantics-preserving transformations (i.e. the code keeps working)... – ...in order to make the code easier to maintain and modify • Refactoring is not just any old restructuring – You need to keep the code working – You need small steps that preserve semantics – You need to have unit tests to prove the code works • There are numerous well-known refactoring techniques – You should be at least somewhat familiar with these before inventing your own
  • 23. Major Refactoring Catalogs • Refactoring: Improving the Design of Existing Code by Martin Fowler with contributions by Kent Beck, John Brant, William Opdyke, and Don Roberts, Addison-Wesley 1999. • Refactoring to Patterns by Joshua Kerievsky, Addison-Wesley 2004. 23
  • 24. Primitive (Basic) Refactorings • Behavior-preserving transformations: – Move Method – Rename Method – Add Class – Extract Method – Pull up Method – … • Can be used as building blocks to create the so-called composite refactorings. – Example: • MoveMethodsToVisitor • Add Template Method 24 See: www.refactoring.com
  • 25. 25 When Should We Refactor • You should refactor: – Any time that you see a better way to do things • “Better” means making the code easier to understand and to modify in the future – You can do so without breaking the code • Unit tests are essential for this – You smell it! (If it stinks, change it) • You should not refactor: – Stable code (code that won’t ever need to change) – Someone else’s code • Unless you’ve inherited it (and now it’s yours)
  • 26. What to refactor Model (High-level) • Not all code smells are design smells! • We need reverse engineering • More abstract, less detail • Can be visualized • Can be hard to reflect the changes to the code (refinement) • Can be automated (more risky) Code (Low-level) • Hard to Understand • Highly Detailed • We can perform Unit testing after refactorings • Can be automated 26
  • 27. 27 Example 1: switch statements • switch statements are very rare in properly designed object-oriented code – Therefore, a switch statement is a simple and easily detected “bad smell” – Of course, not all uses of switch are bad – A switch statement should not be used to distinguish between various kinds of object • There are several well-defined refactorings for this case – The simplest is the creation of subclasses
  • 28. 28 Example 1, continued • class Animal { final int MAMMAL = 0, BIRD = 1, REPTILE = 2; int myKind; // set in constructor ... String getSkin() { switch (myKind) { case MAMMAL: return "hair"; case BIRD: return "feathers"; case REPTILE: return "scales"; default: return "integument"; } } }
  • 29. 29 Example 1, improved • class Animal { String getSkin() { return "integument"; } } class Mammal extends Animal { String getSkin() { return "hair"; } } class Bird extends Animal { String getSkin() { return "feathers"; } } class Reptile extends Animal { String getSkin() { return "scales"; } }
  • 30. 30 How is this an improvement? • Adding a new animal type, such as Insect, does not require revising and recompiling existing code • Mammals, birds, and reptiles are likely to differ in other ways, and we’ve already separated them out (so we won’t need more switch statements) • We’ve gotten rid of the flags we needed to tell one kind of animal from another • Basically, we’re now using Objects the way they were meant to be used
  • 31. 31 JUnit tests • As we refactor, we need to run JUnit tests to ensure that we haven’t introduced errors • public void testGetSkin() { assertEquals("hair", myMammal.getSkin()); assertEquals("feathers", myBird.getSkin()); assertEquals("scales", myReptile.getSkin()); assertEquals("integument", myAnimal.getSkin()); } • This should work equally well with either implementation
  • 32. Refactoring to Patterns • Argue that design patterns can remove code smells • More high level • Each refactoring to patterns is composed of a set of Primitive refactorings • Don’t Forget Testing after refactoring! • 21 refactorings are introduced in: – Refactoring to Patterns by Joshua Kerievsky, Addison- Wesley 2004. 32
  • 33. 33 Catalog of Refactorings to Patterns 1. Replace Constructors with Creation methods 2. Encapsulate Classes with Factory 3. Introduce Polymorphic Creation with Factory Method 4. Replace Conditional Logic with Strategy 5. Form Template Method 6. Compose Method 7. Replace Implicit Tree with Composite 8. Encapsulate Composite with Builder 9. Move Accumulation to Collecting Parameter 10. Extract Composite, Replace one/many with Composite. 11. Replace Conditional Dispatcher with Command 12. Extract Adapter, Unify Interfaces with Adapter 13. Replace Type Code with Class
  • 34. 34 Catalog of Refactorings to Patterns 14. Replace State-Altering Conditionals with State 15. Introduce Null Object 16. Inline Singleton, Limit Instantiation with Singleton 17. Replace Hard-Coded Notifications with Observer 18. Move Embellishment to Decorator, Unify Interfaces, Extract Parameter 19. Move Creation Knowledge to Factory 20. Move Accumulation to Visitor 21. Replace Implicit Language with Interpreter Are these patterns themselves?
  • 35. 35 Each refactoring has • Name and Intent • Gives an application example • Discusses motivation • Benefits and Liabilities • Mechanics – Specific things to do • Presents detailed example
  • 36. How to Use Refactorings • Locate your Code/Design bad smells • Look up for the list of refactorings to resolve that smell. • Example: Conditional Complexity – Replace conditional logic with Strategy – Move embellishment to Decorator – Replace state-altering conditionals with State – (Introduce Null Object) 36
  • 37. How to use the refactorings • Example: Duplicated Code – Form Template Method – Introduce polymorphic creation with Factory Method – (Chain Constructors) – Extract composite – Unify interfaces with Adapter 37
  • 38. Example 2: Duplicated code • If the same code fragment occurs in more than one place within a single class, you can use Extract Method – Turn the fragment into a method whose name explains the purpose of the method – Any local variables that the method requires can be passed as parameters (if there aren’t too many of them!) – If the method changes a local variable, see whether it makes sense to return that as the value of the method (possibly changing the name of the method to indicate this) – If the method changes two or more variables, you need other refactorings to fix this problem
  • 39. Example 2: How to remove Duplicate Code • If the same code fragment occurs in sibling classes, you can use Extract Method in both classes, then use Pull Up Method – Use ExtractMethod in each class – Be sure the code is identical – If necessary, adjust the method signatures to be identical – Copy the extracted method to the common superclass – Delete one subclass method – Compile and test – Delete the other subclass method – Compile and test
  • 40. Example 2: How to remove Duplicate Code II • If the same code fragment occurs in unrelated classes, you can use Extract Method in one class, then: – Use this class as a component in the other class, or – Invoke the method from the other class, or – Move the method to a third class and refer to it from both of the original classes • In any case, you need to decide where the method makes most sense, and put it there
  • 41. Example 2: A Hint • If almost the same code fragment occurs in sibling classes, use Extract Method to separate the similar bits from the different bits, and use Form Template Method
  • 42. Intent of Template Method • Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. • Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. 42
  • 43. The Template Method • Template Methods lead to an inverted control structure – A superclass calls methods in its subclass • Template methods are so fundamental that they can be found in almost every abstract class • Template Method uses inheritance • A similar pattern, Strategy Pattern, uses delegation rather than inheritance
  • 44. Example 2: Big fish and little fish • The scenario: “big fish” and “little fish” move around in an “ocean” – Fish move about randomly – A big fish can move to where a little fish is (and eat it) – A little fish will not move to where a big fish is BigFish move() Fish <<abstract>>move() LittleFish move()
  • 45. Example 2: The move() method • General outline of the method: – public void move() { choose a random direction; // same for both find the location in that direction; // same for both check if it’s ok to move there; // different if it’s ok, make the move; // same for both } • Solution: – Extract the check on whether it’s ok to move – In the Fish class, put the actual (template) move() method – Create an abstract okToMove() method in the Fish class – Implement okToMove() in each subclass
  • 46. The Fish refactoring BigFish move() Fish <<abstract>>move() LittleFish move() BigFish okToMove(locn):boolean Fish move() <<abstract>>okToMove(locn):boolean BigFish okToMove(locn):boolean • Note how this works: When a BigFish tries to move, it uses the move() method in Fish • But the move() method in Fish uses the okToMove(locn) method in BigFish • And similarly for LittleFish
  • 47. Example 3: Replace Constructor with Creation (Factory) Methods • Problem: Constructors on a class make it hard to decide which constructor to call during development. • Solution: Replace the constructors with intention-revealing Creation (Factory) Methods that return object instances . 47
  • 48. Example 3: Class Diagram 48
  • 49. Example 3: Mechanics (Step I) • Find a client that calls a class’s constructor in order to create a kind of instance. • Apply Extract Method on the constructor call to produce a public, static method. • This new method is a creation method. • Apply Move Method to move the creation method to the class containing the chosen constructor. • Compile and test. 49
  • 50. Example 3: Mechanics (Step II) • Find all callers of the chosen constructor that instantiate the same kind of instance as the creation method. • Update them to call the creation method. • Compile and test. 50
  • 51. Example 3: Mechanics (Step III) • If the chosen constructor is chained to another constructor: – Make the creation method call the chained constructor instead of the chosen constructor. • Inline the constructor (Apply Inline method). • Compile and Test. 51
  • 52. Example 3: Mechanics (Final Step) • Repeat steps 1-3 for every constructor on the class that you’d like to turn into Creation Method. • If a constructor on the class has no callers outside the class, make it non-public. • Compile and test. 52
  • 53. 53
  • 54. 54
  • 55. 55
  • 56. Example 3: Benefits and Liabilities • Communicates what kinds of instances are available better than constructors. • Bypasses constructor limitations, such as the inability to have two constructors with the same number and type of arguments. • Makes it easier to find unused creation code. • - Makes creation nonstandard: some classes are instantiated using new, while others use Creation methods. 56
  • 57. Tool Support • There are several CASE tools to automate the Code and Design Refactoring. – Eclipse: has a build in refactoring that supports a set of primitive refactorings. – RefactorIT: supports a large set of refactorings. Also detects some code smells by computing design metrics. Eclipse plugin. – Borland Together: Famous for its design level refactorings to patterns, but expensive! – Some tools exist for VisualStudio, too. 57