SlideShare a Scribd company logo
1 of 23
+
The Dark Art of Refactoring
Systems Analysis and Design
Michael Heron
+
Introduction
 Refactoring is sometimes thought of as a vaguely ‘black art’ of
programming.
 That stems from a basic misunderstanding of what refactoring
is all about.
 Part of this impression comes from a sense of the subjectivity
that goes into refactoring.
 While it’s easy to identify code that is wrong, it’s less easy to
identify which is the ‘best’ out of bits of ‘good’ code.
 The ease at which a system can be refactored is influenced by
the way that system is designed.
+
Refactoring and the Impact of
Change
 Refactoring is an ‘invisible’ action.
 If you do it right, no-one should know you’ve done anything at
all.
 Refactoring is not about adding extra functionality.
 It may be a precursor to this.
 Refactoring is not about fixing bugs.
 Bugs may be fixed as a consequence of it.
 Refactoring is about fixing the problems caused by code you
‘would have done correctly’ given the opportunity.
+
Refactoring and the Impact of
Change
 Refactoring is highly dependent on the impact of change that
goes with your modifications.
 In object orientation, this is predicted by the visibility of your
methods and attributes.
 The benefit of techniques such as abstraction and
encapsulation can readily be seen when it comes time to
refactor code.
 Refactoring an abstracted, encapsulated method is easy.
 Refactoring a system with complex side-effects and distributed
processing is a nightmare.
 Refactoring allows us to redesign architecture in running
programs.
+
The Aim of Refactoring
When we refactor, we’re looking to improve
the quality of the code.
 Remove dead code
 Make inefficient code more efficient
 Make code more readable
 Self-describing code is the best documentation you can
provide.
 Make code more maintainable.
When we refactor, we try to get the code
looking like it would have done, if we’d have
written it properly the first time.
+
A Simple Example
 Consider the following simple class:
public class Example {
private int bing;
public int getValue() {
return bing;
}
public void setValue (int b) {
bing = b;
}
}
 Refactoring can be as simple as changing the name of a
variable to be more meaningful.
+
A Simple (?) Example
 public class Example {
public int bing;
public int getValue() {
return bing;
}
public void setValue (int b) {
bing = b;
}
}
 Depending on the impact of change, even changing a
variable name can be problematic.
+
Impact of Change
 There’s a simple hierarchy we can follow when working out the
impact of change of refactoring:
 Private variables and methods have low impact
 Protected variables and methods have medium impact
 Public variables and methods have high impact.
 Impact of Change relates to the maintainability of your code.
 How much of your code do you have to change?
 As developers, we strive to ensure minimum impact of change.
 You need to labour under the assumption that if someone has
access to a method or variable, they have taken advantage of that.
+
Impact of Change
 Structural elements of a system usually carry with them a high
impact of change.
 It’s usually safe to specialise, it’s usually not safe to generalise.
 In all cases, we want to refactor in such a way that our changes
have no impact on anyone else.
 Fellow developers, mainly.
 All of this relates to the code that we produce at the end of a
systems development project.
 But as a direct consequence is relates to our design.
 We don’t stop designing or analysing just because a system is
‘finished’
+
Another Example
 Let’s say we have a method to which we need to add a
parameter – say we need getValue to accept an integer
parameter:
public class Example {
private int bing;
public int getValue() {
return bing;
}
public void setValue (int b) {
bing = b;
}
}
+
Another Example
 How do we do this? There are two real choices:
 Add the parameter to the method definition.
 High impact of change – every other class making use of
getValue will need to change.
 Add in an overloaded method.
 Low impact of change.
 However, there’s a trade-off here:
 Adding a parameter may require lots of code to change.
 Adding an overloaded method may reduce internal
consistency.
+
Remit of Refactoring
Where does your remit for refactoring lie?
 It depends on how much of the code for which you
are responsible.
 This may extend over a whole program.
 It may extend over a handful of classes.
 You can unilaterally refactor only those elements of
the program for which you have responsibility.
There are few things more frustrating than
finding your programs no longer work
because of someone else’s refactoring…
+
A Third Example
 public class Example {
private int value;
public int makeDesposit (int value, int rate) {
if (value > 100) {
return -1;
}
else if (valid < 0) {
return -1;
}
else {
if (rate < 30) {
value = value * rate;
}
else {
value = value * rate;/2
}
}
}
return value;
}
}
+
Code Aesthetics
 The aesthetics of your code are important.
 They are usually a hint at the maintainability.
 However, it’s important not to just discard
complicated code as needing total refactoring.
 Old code may not be ugly, it may be battle-scarred.
 However, as you gain in experience and
confidence as a developer, you can generally tell
where the bits of code needing attention lie.
 Everyone has their own way of doing this – you
might investigate the Wodehouse Method of
Refactoring for one interesting example.
+
Code Aesthetics
 One way to improve the aesthetics is to break complicated
functionality out into separate methods.
 This fulfils a general rule of object oriented programming, in that
each method should have one responsibility only.
 Complicated and nested structures are usually a good warning
sign of the need to refactor.
 Your activity diagrams will help you understand this.
 Look for lots of loops, lots of branches.
 If you see complicated structures, seek to simplify them.
 Strive to reduce the cyclomatic complexity.
+
Cyclomatic Complexity
 Cyclomatic complexity is a software metric, and is used to
determine how complicated a piece of code is.
 It’s computed using the flow of logic through the constituent
paths of a unit.
 A function, a class, or a whole system.
 The higher the complexity, the more complicated the code.
 The more complicated the code, the harder it is to modify.
 Think of your complexity as ‘the number of decisions made in a
piece of source code’
 It’s more complicated than that, but that’s good enough for now.
+
A Third Example
 public class Example {
public int value;
private boolean getValid (int value) {
if (value > 100 || value < 0) {
return false;
}
return true;
}
private int getRate (int rate) {
int rate;
if (rate > 20 && rate < 30) {
rate = value;
}
else {
rate = value / 2;
}
return rate;
}
public int makeDeposit (int value, int rate) {
int rate;
if (getValid (value) == false) {
return -1;
}
rate = getRate (rate);
value = value * rate;
return value;
}
}
+
Some Common Structual
Refactoring Tasks
 Generalising classes.
 Specialising classes.
 Improving encapsulation.
 Lowering impact of change
 Merging subclasses into fields
 Extracting fields into subclasses
 Implementing interfaces
 Adding abstract classes
 Making better use of polymorphism.
 Reduce inconsistency.
+
Some Common Internal
Refactoring Tasks
 Simplifying internal structures.
 Improving variable names.
 Simplifying logical comparisons
 Substituting one algorithm for another
 Consolidating conditionals
 Extracting functionality into separate methods.
+
Refactoring and Test Driven
Development
 Refactoring introduces no new functionality.
 You can thus use the tests you have put in place previously.
 Having a comprehensive, full test-suite ensures that your
refactored code behaves identically to the previous code.
 The importance of that in a multi-developer environment cannot be
stressed enough.
 We’ve referred to test driven development in the past.
 We write the tests before we write the code, so that when we make
any change we can ensure the whole thing works.
 Refactoring doesn’t violate this rule.
 If it does, you’re not refactoring at all.
+
When Do We Refactor?
Refactoring is an ongoing process we do all
the time.
 Yeah, right.
The ideal case is that we refactor our code
on a continual basis. The realities of life
dictate that we must prioritise.
 Generally, we refactor code that is actively getting
in the way.
 There’s also often a ‘wish list’ we keep as
developers of code that we regret…
+
When Do We Refactor?
 We refactor when code ‘smells bad’
 Code is duplicated across locations.
 There are unjustifiable ‘god objects’
 When cohesion is too low
 When coupling is too high
 When people have been ‘too clever’ for their own good.
 http://www.soberit.hut.fi/mmantyla/BadCodeSme
llsTaxonomy.htm has a good list of ‘bad smells’
+
Conclusion
 Refactoring is the process of looking back at old designs and
fixing them before they become problems.
 Preventative maintenance, in other words.
 You are going to make mistakes when you develop a system.
 Nobody is perfect.
 The ease with which you can fix those mistakes is going to be
based on the designs that you produce.
 Think of refactoring as ‘design band aids’.
 Bearing this in mind at the design stage can save much
heartache in the long run.

More Related Content

What's hot

Beyond Testing: Specs and Behavior Driven Development
Beyond Testing: Specs and Behavior  Driven DevelopmentBeyond Testing: Specs and Behavior  Driven Development
Beyond Testing: Specs and Behavior Driven DevelopmentRabble .
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefAhmed Shreef
 
Prefer Code to Comments
Prefer Code to CommentsPrefer Code to Comments
Prefer Code to CommentsKevlin Henney
 
Software testing lab manual
Software testing lab manualSoftware testing lab manual
Software testing lab manualTanzeem Syed
 
OOPSLA02 BehavioralSemantics.ppt
OOPSLA02 BehavioralSemantics.pptOOPSLA02 BehavioralSemantics.ppt
OOPSLA02 BehavioralSemantics.pptPtidej Team
 
A Study: The Analysis of Test Driven Development And Design Driven Test
A Study: The Analysis of Test Driven Development And Design Driven TestA Study: The Analysis of Test Driven Development And Design Driven Test
A Study: The Analysis of Test Driven Development And Design Driven TestEditor IJMTER
 
Refactoring Techniques
Refactoring TechniquesRefactoring Techniques
Refactoring TechniquesMayada Ghanem
 
Refactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesRefactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesSteven Smith
 

What's hot (13)

Beyond Testing: Specs and Behavior Driven Development
Beyond Testing: Specs and Behavior  Driven DevelopmentBeyond Testing: Specs and Behavior  Driven Development
Beyond Testing: Specs and Behavior Driven Development
 
Sorted
SortedSorted
Sorted
 
BDD Primer
BDD PrimerBDD Primer
BDD Primer
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed Shreef
 
Binding android piece by piece
Binding android piece by pieceBinding android piece by piece
Binding android piece by piece
 
Prefer Code to Comments
Prefer Code to CommentsPrefer Code to Comments
Prefer Code to Comments
 
Inside Requirements
Inside RequirementsInside Requirements
Inside Requirements
 
Software testing lab manual
Software testing lab manualSoftware testing lab manual
Software testing lab manual
 
OOPSLA02 BehavioralSemantics.ppt
OOPSLA02 BehavioralSemantics.pptOOPSLA02 BehavioralSemantics.ppt
OOPSLA02 BehavioralSemantics.ppt
 
Block 1 ms-034 unit-1
Block 1 ms-034 unit-1Block 1 ms-034 unit-1
Block 1 ms-034 unit-1
 
A Study: The Analysis of Test Driven Development And Design Driven Test
A Study: The Analysis of Test Driven Development And Design Driven TestA Study: The Analysis of Test Driven Development And Design Driven Test
A Study: The Analysis of Test Driven Development And Design Driven Test
 
Refactoring Techniques
Refactoring TechniquesRefactoring Techniques
Refactoring Techniques
 
Refactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesRefactoring Applications using SOLID Principles
Refactoring Applications using SOLID Principles
 

Similar to SAD10 - Refactoring

Introduction to Refactoring
Introduction to RefactoringIntroduction to Refactoring
Introduction to RefactoringVorleak Chy
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2Hammad Rajjoub
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2Hammad Rajjoub
 
Agile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddAgile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddSrinivasa GV
 
How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?Sandro Mancuso
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019Paulo Clavijo
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Hammad Rajjoub
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review ProcessDr. Syed Hassan Amin
 
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...Abdelkrim Boujraf
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringEyob Lube
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Robin O'Brien
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardMax Kleiner
 
SAD05 - Encapsulation
SAD05 - EncapsulationSAD05 - Encapsulation
SAD05 - EncapsulationMichael Heron
 

Similar to SAD10 - Refactoring (20)

Introduction to Refactoring
Introduction to RefactoringIntroduction to Refactoring
Introduction to Refactoring
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
Agile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddAgile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tdd
 
How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Put to the Test
Put to the TestPut to the Test
Put to the Test
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Facade Design Pattern
Facade Design PatternFacade Design Pattern
Facade Design Pattern
 
Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Combating software entropy 2-roc1-
Combating software entropy 2-roc1-
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
 
SAD05 - Encapsulation
SAD05 - EncapsulationSAD05 - Encapsulation
SAD05 - Encapsulation
 

More from Michael Heron

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMichael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconductMichael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkMichael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility SupportMichael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and AutershipMichael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interactionMichael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityMichael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - TexturesMichael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationMichael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsMichael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsMichael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationMichael Heron
 

More from Michael Heron (20)

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - Inheritance
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 

Recently uploaded

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 

SAD10 - Refactoring

  • 1. + The Dark Art of Refactoring Systems Analysis and Design Michael Heron
  • 2. + Introduction  Refactoring is sometimes thought of as a vaguely ‘black art’ of programming.  That stems from a basic misunderstanding of what refactoring is all about.  Part of this impression comes from a sense of the subjectivity that goes into refactoring.  While it’s easy to identify code that is wrong, it’s less easy to identify which is the ‘best’ out of bits of ‘good’ code.  The ease at which a system can be refactored is influenced by the way that system is designed.
  • 3. + Refactoring and the Impact of Change  Refactoring is an ‘invisible’ action.  If you do it right, no-one should know you’ve done anything at all.  Refactoring is not about adding extra functionality.  It may be a precursor to this.  Refactoring is not about fixing bugs.  Bugs may be fixed as a consequence of it.  Refactoring is about fixing the problems caused by code you ‘would have done correctly’ given the opportunity.
  • 4. + Refactoring and the Impact of Change  Refactoring is highly dependent on the impact of change that goes with your modifications.  In object orientation, this is predicted by the visibility of your methods and attributes.  The benefit of techniques such as abstraction and encapsulation can readily be seen when it comes time to refactor code.  Refactoring an abstracted, encapsulated method is easy.  Refactoring a system with complex side-effects and distributed processing is a nightmare.  Refactoring allows us to redesign architecture in running programs.
  • 5. + The Aim of Refactoring When we refactor, we’re looking to improve the quality of the code.  Remove dead code  Make inefficient code more efficient  Make code more readable  Self-describing code is the best documentation you can provide.  Make code more maintainable. When we refactor, we try to get the code looking like it would have done, if we’d have written it properly the first time.
  • 6. + A Simple Example  Consider the following simple class: public class Example { private int bing; public int getValue() { return bing; } public void setValue (int b) { bing = b; } }  Refactoring can be as simple as changing the name of a variable to be more meaningful.
  • 7. + A Simple (?) Example  public class Example { public int bing; public int getValue() { return bing; } public void setValue (int b) { bing = b; } }  Depending on the impact of change, even changing a variable name can be problematic.
  • 8. + Impact of Change  There’s a simple hierarchy we can follow when working out the impact of change of refactoring:  Private variables and methods have low impact  Protected variables and methods have medium impact  Public variables and methods have high impact.  Impact of Change relates to the maintainability of your code.  How much of your code do you have to change?  As developers, we strive to ensure minimum impact of change.  You need to labour under the assumption that if someone has access to a method or variable, they have taken advantage of that.
  • 9. + Impact of Change  Structural elements of a system usually carry with them a high impact of change.  It’s usually safe to specialise, it’s usually not safe to generalise.  In all cases, we want to refactor in such a way that our changes have no impact on anyone else.  Fellow developers, mainly.  All of this relates to the code that we produce at the end of a systems development project.  But as a direct consequence is relates to our design.  We don’t stop designing or analysing just because a system is ‘finished’
  • 10. + Another Example  Let’s say we have a method to which we need to add a parameter – say we need getValue to accept an integer parameter: public class Example { private int bing; public int getValue() { return bing; } public void setValue (int b) { bing = b; } }
  • 11. + Another Example  How do we do this? There are two real choices:  Add the parameter to the method definition.  High impact of change – every other class making use of getValue will need to change.  Add in an overloaded method.  Low impact of change.  However, there’s a trade-off here:  Adding a parameter may require lots of code to change.  Adding an overloaded method may reduce internal consistency.
  • 12. + Remit of Refactoring Where does your remit for refactoring lie?  It depends on how much of the code for which you are responsible.  This may extend over a whole program.  It may extend over a handful of classes.  You can unilaterally refactor only those elements of the program for which you have responsibility. There are few things more frustrating than finding your programs no longer work because of someone else’s refactoring…
  • 13. + A Third Example  public class Example { private int value; public int makeDesposit (int value, int rate) { if (value > 100) { return -1; } else if (valid < 0) { return -1; } else { if (rate < 30) { value = value * rate; } else { value = value * rate;/2 } } } return value; } }
  • 14. + Code Aesthetics  The aesthetics of your code are important.  They are usually a hint at the maintainability.  However, it’s important not to just discard complicated code as needing total refactoring.  Old code may not be ugly, it may be battle-scarred.  However, as you gain in experience and confidence as a developer, you can generally tell where the bits of code needing attention lie.  Everyone has their own way of doing this – you might investigate the Wodehouse Method of Refactoring for one interesting example.
  • 15. + Code Aesthetics  One way to improve the aesthetics is to break complicated functionality out into separate methods.  This fulfils a general rule of object oriented programming, in that each method should have one responsibility only.  Complicated and nested structures are usually a good warning sign of the need to refactor.  Your activity diagrams will help you understand this.  Look for lots of loops, lots of branches.  If you see complicated structures, seek to simplify them.  Strive to reduce the cyclomatic complexity.
  • 16. + Cyclomatic Complexity  Cyclomatic complexity is a software metric, and is used to determine how complicated a piece of code is.  It’s computed using the flow of logic through the constituent paths of a unit.  A function, a class, or a whole system.  The higher the complexity, the more complicated the code.  The more complicated the code, the harder it is to modify.  Think of your complexity as ‘the number of decisions made in a piece of source code’  It’s more complicated than that, but that’s good enough for now.
  • 17. + A Third Example  public class Example { public int value; private boolean getValid (int value) { if (value > 100 || value < 0) { return false; } return true; } private int getRate (int rate) { int rate; if (rate > 20 && rate < 30) { rate = value; } else { rate = value / 2; } return rate; } public int makeDeposit (int value, int rate) { int rate; if (getValid (value) == false) { return -1; } rate = getRate (rate); value = value * rate; return value; } }
  • 18. + Some Common Structual Refactoring Tasks  Generalising classes.  Specialising classes.  Improving encapsulation.  Lowering impact of change  Merging subclasses into fields  Extracting fields into subclasses  Implementing interfaces  Adding abstract classes  Making better use of polymorphism.  Reduce inconsistency.
  • 19. + Some Common Internal Refactoring Tasks  Simplifying internal structures.  Improving variable names.  Simplifying logical comparisons  Substituting one algorithm for another  Consolidating conditionals  Extracting functionality into separate methods.
  • 20. + Refactoring and Test Driven Development  Refactoring introduces no new functionality.  You can thus use the tests you have put in place previously.  Having a comprehensive, full test-suite ensures that your refactored code behaves identically to the previous code.  The importance of that in a multi-developer environment cannot be stressed enough.  We’ve referred to test driven development in the past.  We write the tests before we write the code, so that when we make any change we can ensure the whole thing works.  Refactoring doesn’t violate this rule.  If it does, you’re not refactoring at all.
  • 21. + When Do We Refactor? Refactoring is an ongoing process we do all the time.  Yeah, right. The ideal case is that we refactor our code on a continual basis. The realities of life dictate that we must prioritise.  Generally, we refactor code that is actively getting in the way.  There’s also often a ‘wish list’ we keep as developers of code that we regret…
  • 22. + When Do We Refactor?  We refactor when code ‘smells bad’  Code is duplicated across locations.  There are unjustifiable ‘god objects’  When cohesion is too low  When coupling is too high  When people have been ‘too clever’ for their own good.  http://www.soberit.hut.fi/mmantyla/BadCodeSme llsTaxonomy.htm has a good list of ‘bad smells’
  • 23. + Conclusion  Refactoring is the process of looking back at old designs and fixing them before they become problems.  Preventative maintenance, in other words.  You are going to make mistakes when you develop a system.  Nobody is perfect.  The ease with which you can fix those mistakes is going to be based on the designs that you produce.  Think of refactoring as ‘design band aids’.  Bearing this in mind at the design stage can save much heartache in the long run.