SlideShare a Scribd company logo
What is Refactoring
Refactoring is the process of changing a software system in
such a way that it does not alter the external behavior of the
code yet improves its internal structure.
Why refactor?
● When you need to either fix a bug or add a new feature,
then implementing the code necessary would be a lot
easier once you had refactored it to be in a more stable,
and understandable state.
● The most suitable time to start refactoring is when you are
fixing bugs or adding a new feature, and it should be done
during the life cycle of the project not at the end.
Testing while Refactoring
● Its important to perform regular tests during refactoring to
make sure that you do not break anything during
refactoring as refactoring main concern is not to change
the API.
Techniques
1-Rename methods:
Giving methods/attributes names that reduce the need of the
comments and help to promote greater clarity.
2-Introduce explaining variables:
Expressions can become very complex and hard to read. In such
situations temporary variables can be helpful to break down the
expression into something more manageable.
Example:
if ((platform.upcase().index("MAC") > -1) should become
– isMacOs = platform.upcase().index("MAC") > -1
– if (isMacOs)
Techniques
3-Inline Temp:
temp variables that makes method longer and they only used once and
they are simple expressions.
– Inline Temp effectively removes the temp variable altogether by just
using the value assigned to it.
– Example:
– temp_variable_with_descriptive_name = add_stuff
puts "Number is #{temp_variable_with_descriptive_name}"
– Becomes "Number is #add_stuff}"
Techniques
4-Split Temp Variable:
– if a temporary variable is assigned to more than once and it is not a loop
variable or a collecting/accumulator variable then it is a temp considered to
have too many responsibilities.
– Example:
– Temp = a+b
– Temp = a-b should become
– Sum = a + b
– Diff = a - b
5-Replace Temp With Query:
You are using a temporary variable to hold the result of an expression.
– Complex expression assigned to the temp needs to be first moved to a
method.
–
Techniques
Example:
def volume
# `area` is the temp
area = length * width
area * height
end
Should become
def volume
# notice `area` is now a direct method call
area * height
end
def area
length * width
end
●
●
Techniques
6-Replace Temp With Chain:
f you have a temp variable holding the result of calling an object's method, and
follow the assignment by using that temp to carry out more method calls, then you
should consider chaining method calls instead by making sure that every function
returns self of the object.
– Example:
– temp = College.new
– temp.create_course
– temp.add_student
– Should become
– college = College.create_course
– .add_student
Techniques
7-Extract Method:
It consists of breaking up long methods by shifting overly complex chunks of
code into new methods which have very descriptive identifiers.
– Example:
– Def printOwing
– printBanner
–
– //print details
– Puts "name: " + _name
– Puts "amount " + getOutstanding
– end
Should become
– Def printOwing
– printBanner
– printDetails(getOutstanding())
– end
–
– def printDetails ( outstanding)
– Puts "name: " + _name
– Puts "amount " + outstanding
– end
–
Techniques
8-Inline Method:
if method is not used more than once and perform a very simple line of code so we
will do the exact opposite of extract method.
9-Move Method:
If a method is asking another class a lot of questions then it may be an
indication the method is on the wrong object.
– So we should move the method to the other class and change the
required differences in the methhods
10-Replace Method With Method Object:
you have a long method you want to use Extract Method on, but the number of
temporary local variables are too great to allow you to utilise the Extract Method
technique (because passing around that many variables would be just as messy as
the long method itself.
Techniques
11-Replace Loop With Collection Closure Method:
you hide the ugly details of the loop behind a nicer iteration method, allowing the
developer looking at the code to focus on the business logic instead.
– Example:
– managers = []
– employees.each do |e|
– managers << e if e.manager?
– End
– Should becomes
– managers = employees.select { |e| e.manager? }
12-Pull Up Method:
When you have duplicated code across two separate classes then the best
refactoring technique to implement is to pull that duplicate code up into a super class.
(oop concpet)
Techniques
13-Form Template Method:
You have two methods in subclasses that perform similar steps in the
same order, yet the steps are different.
– Get the steps into methods with the same signature, so that the original
methods become the same. Then you can pull them up.(OOP Concept)
14-Extract Surrounding Method:
If you find you have different methods which contain almost identical
code but with a slight variant in the middle, then pull up the duplicated
code into a single method and pass a code block to the newly created
method.
Techniques
15-Self Encapsulate Field:
When inheriting properties from a parent class/object then it can be more
flexible if the parent class only allows access to the properties from
within a getter/setter.(OOP Concept)
16-Introduce Named Parameter:
When method arguments are unclear then convert them into named
parameters so they become clearer.
– Example:
def turnOnTheTV (channel: 1, volume: 1); end
– turnOnTheTV(channel: 101, volume: 10)

More Related Content

PDF
Code Refactoring
PDF
Refactoring 101
PPTX
Code refactoring
PPT
Refactoring Tips by Martin Fowler
PDF
Refactoring
PDF
Refactoring
PPTX
Refactoring and code smells
PDF
Refactoring: Improve the design of existing code
Code Refactoring
Refactoring 101
Code refactoring
Refactoring Tips by Martin Fowler
Refactoring
Refactoring
Refactoring and code smells
Refactoring: Improve the design of existing code

What's hot (20)

PDF
Code Smells and Its type (With Example)
PPTX
Code smells and remedies
PPTX
Code smell overview
PDF
Refactoring
PPTX
Tech talks#6: Code Refactoring
PPTX
Code Smell, Software Engineering
PDF
Bad Code Smells
PPTX
Decomposition using Functional Dependency
PPTX
Spiral Model
PPTX
The Art of Debugging.pptx
PPTX
Design pattern-presentation
PPTX
Prototype Model
PPTX
Python: Polymorphism
PDF
Integration testing with spring @snow one
PDF
Software requirements
PPT
SOLID Design Principles
PPTX
PPTX
SOLID Principles
PPTX
Cohesion and coupling
Code Smells and Its type (With Example)
Code smells and remedies
Code smell overview
Refactoring
Tech talks#6: Code Refactoring
Code Smell, Software Engineering
Bad Code Smells
Decomposition using Functional Dependency
Spiral Model
The Art of Debugging.pptx
Design pattern-presentation
Prototype Model
Python: Polymorphism
Integration testing with spring @snow one
Software requirements
SOLID Design Principles
SOLID Principles
Cohesion and coupling
Ad

Viewers also liked (7)

PPTX
The Value of Refactoring on an Agile Team
PDF
Riding the Agile Wave
PDF
Introduction to Refactoring
PPT
Agile Development Ultimate Slides
PPTX
Estimating and planning Agile projects
PPTX
Release Management: Successful Software Releases Start with a Plan
PPTX
Agile Release Planning
The Value of Refactoring on an Agile Team
Riding the Agile Wave
Introduction to Refactoring
Agile Development Ultimate Slides
Estimating and planning Agile projects
Release Management: Successful Software Releases Start with a Plan
Agile Release Planning
Ad

Similar to Refactoring Techniques (20)

PDF
Refactoring
PDF
Refactoring Workshop (Rails Pacific 2014)
PDF
Martin Fowler's Refactoring Techniques Quick Reference
PPT
Bad Smell in Codes - Part 1
PDF
Refactoring Long Methods
PDF
So You Want to Start Refactoring?
PDF
Bad Smell In Codes 1
PPTX
Code smells
PPTX
Refactoring
PPTX
Introduction to Refactoring
PPTX
Code Refactoring
PPT
Agile development with Ruby
PDF
Simplifying Code: Monster to Elegant in 5 Steps
PPTX
31 days Refactoring
PPTX
Bad Smells in Code
PPT
Code Refactoring - 3.0
PPTX
Refactoring Chapter 6,7.pptx
PDF
Bade Smells in Code
PPTX
Agile korea 2013 유석문
PPTX
Refactoring
Refactoring
Refactoring Workshop (Rails Pacific 2014)
Martin Fowler's Refactoring Techniques Quick Reference
Bad Smell in Codes - Part 1
Refactoring Long Methods
So You Want to Start Refactoring?
Bad Smell In Codes 1
Code smells
Refactoring
Introduction to Refactoring
Code Refactoring
Agile development with Ruby
Simplifying Code: Monster to Elegant in 5 Steps
31 days Refactoring
Bad Smells in Code
Code Refactoring - 3.0
Refactoring Chapter 6,7.pptx
Bade Smells in Code
Agile korea 2013 유석문
Refactoring

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Pre independence Education in Inndia.pdf
PPTX
master seminar digital applications in india
PPTX
Cell Types and Its function , kingdom of life
PDF
01-Introduction-to-Information-Management.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Pharma ospi slides which help in ospi learning
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Business Ethics Teaching Materials for college
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Abdominal Access Techniques with Prof. Dr. R K Mishra
VCE English Exam - Section C Student Revision Booklet
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
O7-L3 Supply Chain Operations - ICLT Program
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Complications of Minimal Access Surgery at WLH
Pre independence Education in Inndia.pdf
master seminar digital applications in india
Cell Types and Its function , kingdom of life
01-Introduction-to-Information-Management.pdf
Classroom Observation Tools for Teachers
Pharma ospi slides which help in ospi learning
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Renaissance Architecture: A Journey from Faith to Humanism
Anesthesia in Laparoscopic Surgery in India
Business Ethics Teaching Materials for college
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester

Refactoring Techniques

  • 1. What is Refactoring Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure.
  • 2. Why refactor? ● When you need to either fix a bug or add a new feature, then implementing the code necessary would be a lot easier once you had refactored it to be in a more stable, and understandable state. ● The most suitable time to start refactoring is when you are fixing bugs or adding a new feature, and it should be done during the life cycle of the project not at the end.
  • 3. Testing while Refactoring ● Its important to perform regular tests during refactoring to make sure that you do not break anything during refactoring as refactoring main concern is not to change the API.
  • 4. Techniques 1-Rename methods: Giving methods/attributes names that reduce the need of the comments and help to promote greater clarity. 2-Introduce explaining variables: Expressions can become very complex and hard to read. In such situations temporary variables can be helpful to break down the expression into something more manageable. Example: if ((platform.upcase().index("MAC") > -1) should become – isMacOs = platform.upcase().index("MAC") > -1 – if (isMacOs)
  • 5. Techniques 3-Inline Temp: temp variables that makes method longer and they only used once and they are simple expressions. – Inline Temp effectively removes the temp variable altogether by just using the value assigned to it. – Example: – temp_variable_with_descriptive_name = add_stuff puts "Number is #{temp_variable_with_descriptive_name}" – Becomes "Number is #add_stuff}"
  • 6. Techniques 4-Split Temp Variable: – if a temporary variable is assigned to more than once and it is not a loop variable or a collecting/accumulator variable then it is a temp considered to have too many responsibilities. – Example: – Temp = a+b – Temp = a-b should become – Sum = a + b – Diff = a - b 5-Replace Temp With Query: You are using a temporary variable to hold the result of an expression. – Complex expression assigned to the temp needs to be first moved to a method. –
  • 7. Techniques Example: def volume # `area` is the temp area = length * width area * height end Should become def volume # notice `area` is now a direct method call area * height end def area length * width end ● ●
  • 8. Techniques 6-Replace Temp With Chain: f you have a temp variable holding the result of calling an object's method, and follow the assignment by using that temp to carry out more method calls, then you should consider chaining method calls instead by making sure that every function returns self of the object. – Example: – temp = College.new – temp.create_course – temp.add_student – Should become – college = College.create_course – .add_student
  • 9. Techniques 7-Extract Method: It consists of breaking up long methods by shifting overly complex chunks of code into new methods which have very descriptive identifiers. – Example: – Def printOwing – printBanner – – //print details – Puts "name: " + _name – Puts "amount " + getOutstanding – end Should become – Def printOwing – printBanner – printDetails(getOutstanding()) – end – – def printDetails ( outstanding) – Puts "name: " + _name – Puts "amount " + outstanding – end –
  • 10. Techniques 8-Inline Method: if method is not used more than once and perform a very simple line of code so we will do the exact opposite of extract method. 9-Move Method: If a method is asking another class a lot of questions then it may be an indication the method is on the wrong object. – So we should move the method to the other class and change the required differences in the methhods 10-Replace Method With Method Object: you have a long method you want to use Extract Method on, but the number of temporary local variables are too great to allow you to utilise the Extract Method technique (because passing around that many variables would be just as messy as the long method itself.
  • 11. Techniques 11-Replace Loop With Collection Closure Method: you hide the ugly details of the loop behind a nicer iteration method, allowing the developer looking at the code to focus on the business logic instead. – Example: – managers = [] – employees.each do |e| – managers << e if e.manager? – End – Should becomes – managers = employees.select { |e| e.manager? } 12-Pull Up Method: When you have duplicated code across two separate classes then the best refactoring technique to implement is to pull that duplicate code up into a super class. (oop concpet)
  • 12. Techniques 13-Form Template Method: You have two methods in subclasses that perform similar steps in the same order, yet the steps are different. – Get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up.(OOP Concept) 14-Extract Surrounding Method: If you find you have different methods which contain almost identical code but with a slight variant in the middle, then pull up the duplicated code into a single method and pass a code block to the newly created method.
  • 13. Techniques 15-Self Encapsulate Field: When inheriting properties from a parent class/object then it can be more flexible if the parent class only allows access to the properties from within a getter/setter.(OOP Concept) 16-Introduce Named Parameter: When method arguments are unclear then convert them into named parameters so they become clearer. – Example: def turnOnTheTV (channel: 1, volume: 1); end – turnOnTheTV(channel: 101, volume: 10)