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
Refactoring Chapter 6,7.pptx
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.
Refactoring Chapter 6,7.pptx
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

PDF
Design patterns
Anas Alpure
 
PDF
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
PDF
The Ring programming language version 1.5.4 book - Part 74 of 185
Mahmoud Samir Fayed
 
PDF
Java performance
Rajesuwer P. Singaravelu
 
DOC
Delphi qa
sandy14234
 
PDF
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
eagac2004
 
PPTX
Implementing the Adapter Design Pattern
ProdigyView
 
PPTX
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
mru761077
 
Design patterns
Anas Alpure
 
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
The Ring programming language version 1.5.4 book - Part 74 of 185
Mahmoud Samir Fayed
 
Java performance
Rajesuwer P. Singaravelu
 
Delphi qa
sandy14234
 
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
eagac2004
 
Implementing the Adapter Design Pattern
ProdigyView
 
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
mru761077
 

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

PPTX
Design Patterns
Ankit.Rustagi
 
PPT
Md02 - Getting Started part-2
Rakesh Madugula
 
PPT
Code Refactoring
Charlie Berg
 
PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
PPT
Refactoring Tips by Martin Fowler
Igor Crvenov
 
ODP
Refactoring Techniques
Mayada Ghanem
 
ODP
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
PDF
JAVA-PPT'S.pdf
AnmolVerma363503
 
PDF
Week 1 - 4 summary slides.pdfhxbsnsnanbsbs
txdm2597
 
PPTX
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
PDF
C# Advanced L07-Design Patterns
Mohammad Shaker
 
PPT
Object Oriented Concepts and Principles
deonpmeyer
 
PPT
Database structure Structures Link list and trees and Recurison complete
Adnan abid
 
PPT
Oops concepts in php
CPD INDIA
 
PPT
Reflection
Piyush Mittal
 
PDF
Observer pattern
Somenath Mukhopadhyay
 
PPTX
Chapter One Function.pptx
miki304759
 
PPTX
Agile korea 2013 유석문
Sangcheol Hwang
 
PPTX
OOPS IN PHP.pptx
rani marri
 
PDF
Abap object-oriented-programming-tutorials
cesarmendez78
 
Design Patterns
Ankit.Rustagi
 
Md02 - Getting Started part-2
Rakesh Madugula
 
Code Refactoring
Charlie Berg
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
Refactoring Tips by Martin Fowler
Igor Crvenov
 
Refactoring Techniques
Mayada Ghanem
 
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
JAVA-PPT'S.pdf
AnmolVerma363503
 
Week 1 - 4 summary slides.pdfhxbsnsnanbsbs
txdm2597
 
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
C# Advanced L07-Design Patterns
Mohammad Shaker
 
Object Oriented Concepts and Principles
deonpmeyer
 
Database structure Structures Link list and trees and Recurison complete
Adnan abid
 
Oops concepts in php
CPD INDIA
 
Reflection
Piyush Mittal
 
Observer pattern
Somenath Mukhopadhyay
 
Chapter One Function.pptx
miki304759
 
Agile korea 2013 유석문
Sangcheol Hwang
 
OOPS IN PHP.pptx
rani marri
 
Abap object-oriented-programming-tutorials
cesarmendez78
 
Ad

Recently uploaded (20)

PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Ad

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.
  • 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.
  • 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