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)

Refactoring Techniques

  • 1.
    What is Refactoring Refactoringis 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? ● Whenyou 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/attributesnames 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 variablesthat 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 WithChain: 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 consistsof 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 methodis 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 WithCollection 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: Youhave 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: Wheninheriting 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)