SlideShare a Scribd company logo
Refactoring:
Improving the Design
of Existing Code 2nd
Edition
Chapter 6: A first Set of
Refactorings
Chapter 7: Encapsulation
28 July 2022
Efekan AYDEMIR
İ2i Systems Intern
Chapter 6: A First
Set of Refactorings
- Extract Function
- Inline Function
- Extract Variable
- Inline Variable
- Change Function Declaration
- Encapsulate Variable
A Good Refactoring Cycle
Extract Function
- Our aim is putting repeatedly used codes into a function.
- Name it with its job.
- Even it is a single line function you better extract it into function.
- But code only used once should be left inline.
Inline Function
- Sometimes, we do
come across a function
in which the body is as
clear as the name
- In such situations,
there is no need to
extract the code.
Extract Variable
- It means add a name to
an expression in my code.
- When it comes to
debugging, it will be
easier to investigate.
Inline Variable
Variables provide names for
expressions within a function, and
as such they are usually a Good
Thing.
But sometimes, the name doesn’t
really communicate more than the
expression itself.
Change Function Declaration
Encapsulate Variables
- If we move data around, we have to
change all the references so te data in
a single cycle to keep the coeee
working
- As the scope grows, so does the
difficulty, which is why global data is
such a pain.
INTRODUCE PARAMETER OBJECT
Your methods contain a repeating
group of parameters.
Replace these parameters with
an object.
Combine Functions Into Transform
Use a data transformation function that
takes the source data as input and
calculates all the derivations, putting each
derived value as a field in the output data.
- Then, to examine the derivations, all I
need do is look at the transform function.
Split Phase
-In a split phase refactoring,
we split a tangled piece of
code that does more than one
thing into distinguished
phases that only do a single
thing.
- As a result, we can more
easily and independently
change each of the resulting
phases.
Chapter 7:
Encapsulation
- Encapsulate Record
- Encapsulate Collection
- Replace Primitive with Object
- Replace Temp with Query
- Extract Class
- Inline Class
- Hide Delegate
- Remove Middle Man
- Substitute Algorithm
Encapsulate Record (Class)
- Create record (class) from object
- Hide what's stored and provide
methods to get value.
- Makes it easier for refactoring
- People can change variables
directly
Encapsulate Collection
A class contains a collection field
and a simple getter and setter for
working with the collection.
- Make the getter-returned value read-
only and create methods for
adding/deleting elements of the
collection.
Replace Primitive with Object
- A class (or group of classes) contains a data field. The field
has its own behavior and associated data.
- Create a new class, place the old field and its behavior in the
class, and store the object of the class in the original class.
Replace Temp with Query
- You place the result of an expression in
a local variable for later use in your code.
- Move the entire expression
to a separate method and
return the result from it.
Extract Class
- When one class does the work
of two, awkwardness results.
- Instead, create a new class and place
the fields and methods responsible for
the relevant functionality in it.
Inline Class
- A class does almost nothing and isn’t responsible for
anything, and no additional responsibilities are
planned for it.
- Move all features from the class to another one.
Hide Delegate
- Calling another object through a delegate.
Remove Middle Man
Substitute Function
So you want to replace an existing
algorithm with a new one?
Replace the body of the method that
implements the algorithm with a new
algorithm.
References
• https://refactoring.guru/
• The Book, Refactoring: Improving the Design of Existing Code by
Martin Fowler
• https://github.com/ittus/Refactoring-summary-2nd-javascript
THANKS FOR LİSTENİNG
Contact me : Aydemir081@gmail.com

More Related Content

Similar to Refactoring Chapter 6,7.pptx

Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
Rakesh Madugula
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
Charlie Berg
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
Igor Crvenov
 
Refactoring Techniques
Refactoring TechniquesRefactoring Techniques
Refactoring Techniques
Mayada Ghanem
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
AnmolVerma363503
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design Patterns
Mohammad Shaker
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
deonpmeyer
 
Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete  Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete
Adnan abid
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
Reflection
ReflectionReflection
Reflection
Piyush Mittal
 
Observer pattern
Observer patternObserver pattern
Observer pattern
Somenath Mukhopadhyay
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문
Sangcheol Hwang
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
rani marri
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
cesarmendez78
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
Abner Chih Yi Huang
 

Similar to Refactoring Chapter 6,7.pptx (20)

Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Refactoring Techniques
Refactoring TechniquesRefactoring Techniques
Refactoring Techniques
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design Patterns
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete  Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Reflection
ReflectionReflection
Reflection
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
 

Recently uploaded

How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
Celine George
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
nitinpv4ai
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
ImMuslim
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
Celine George
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
blueshagoo1
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
National Information Standards Organization (NISO)
 

Recently uploaded (20)

How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
 

Refactoring Chapter 6,7.pptx

  • 1. Refactoring: Improving the Design of Existing Code 2nd Edition Chapter 6: A first Set of Refactorings Chapter 7: Encapsulation 28 July 2022 Efekan AYDEMIR İ2i Systems Intern
  • 2. Chapter 6: A First Set of Refactorings - Extract Function - Inline Function - Extract Variable - Inline Variable - Change Function Declaration - Encapsulate Variable
  • 4. Extract Function - Our aim is putting repeatedly used codes into a function. - Name it with its job. - Even it is a single line function you better extract it into function. - But code only used once should be left inline.
  • 5. Inline Function - Sometimes, we do come across a function in which the body is as clear as the name - In such situations, there is no need to extract the code.
  • 6. Extract Variable - It means add a name to an expression in my code. - When it comes to debugging, it will be easier to investigate.
  • 7. Inline Variable Variables provide names for expressions within a function, and as such they are usually a Good Thing. But sometimes, the name doesn’t really communicate more than the expression itself.
  • 9.
  • 10. Encapsulate Variables - If we move data around, we have to change all the references so te data in a single cycle to keep the coeee working - As the scope grows, so does the difficulty, which is why global data is such a pain.
  • 11. INTRODUCE PARAMETER OBJECT Your methods contain a repeating group of parameters. Replace these parameters with an object.
  • 12. Combine Functions Into Transform Use a data transformation function that takes the source data as input and calculates all the derivations, putting each derived value as a field in the output data. - Then, to examine the derivations, all I need do is look at the transform function.
  • 13. Split Phase -In a split phase refactoring, we split a tangled piece of code that does more than one thing into distinguished phases that only do a single thing. - As a result, we can more easily and independently change each of the resulting phases.
  • 14. Chapter 7: Encapsulation - Encapsulate Record - Encapsulate Collection - Replace Primitive with Object - Replace Temp with Query - Extract Class - Inline Class - Hide Delegate - Remove Middle Man - Substitute Algorithm
  • 15. Encapsulate Record (Class) - Create record (class) from object - Hide what's stored and provide methods to get value. - Makes it easier for refactoring - People can change variables directly
  • 16. Encapsulate Collection A class contains a collection field and a simple getter and setter for working with the collection. - Make the getter-returned value read- only and create methods for adding/deleting elements of the collection.
  • 17. Replace Primitive with Object - A class (or group of classes) contains a data field. The field has its own behavior and associated data. - Create a new class, place the old field and its behavior in the class, and store the object of the class in the original class.
  • 18. Replace Temp with Query - You place the result of an expression in a local variable for later use in your code. - Move the entire expression to a separate method and return the result from it.
  • 19. Extract Class - When one class does the work of two, awkwardness results. - Instead, create a new class and place the fields and methods responsible for the relevant functionality in it.
  • 20. Inline Class - A class does almost nothing and isn’t responsible for anything, and no additional responsibilities are planned for it. - Move all features from the class to another one.
  • 21. Hide Delegate - Calling another object through a delegate.
  • 23. Substitute Function So you want to replace an existing algorithm with a new one? Replace the body of the method that implements the algorithm with a new algorithm.
  • 24.
  • 25. References • https://refactoring.guru/ • The Book, Refactoring: Improving the Design of Existing Code by Martin Fowler • https://github.com/ittus/Refactoring-summary-2nd-javascript
  • 26. THANKS FOR LİSTENİNG Contact me : Aydemir081@gmail.com