Refactoring Flashcards
What are the 3 most important steps for refactoring?
At the most abstract level, you’re looking for three things: complexity to break up, duplication to combine, and abstractions waiting to be born
What is code smell?
Code smells are warning signs about potential problems in code.
What is refactoring?
It’s changing code, without changing business logic in order to improve readability and the ability to update code easier.
When we should avoid doing refactoring?
- Project almost finished
- This place won’t be changed in the future
- Renaming of the open interface ( public API ) - Be careful
What is Extract Method technique?
Allows us to translate code in method and this method explains the purpose of the code.
We have:
puts ‘Something’
puts ‘Something 2’
puts ‘Something 3 ‘
We translate to:
def print_details puts 'Something' puts 'Something 2' puts 'Something 3 ' end
What is Inline method technique?
When the method’s body is straightforward and you don’t need to explain it via method then you can remove this method.
We have:
def more_than_five(number)
number > 5
end
We translate to:
number > 5
What is Inline Temp variable technique?
We can remove the variable if we use it only once.
We have:
price = Sample.price
return price > 0
We translate to:
return Sample.price > 0
What is Replace Temp Variable with Query technique?
When we replace variable with a method.
We have:
price = one_value * second_value
return price > 0
We translate to:
def price
one_value * second_value
end
return price > 0
What is Introduce Explaining variable technique?
When we moving the difficult part under the variable
We have
def hello(a, b)
b2 + (2ab)
end
We translate to
def hello(a, b) calculation1 = b**2 calculation2 = (2*a*b)
calculation1 + calculation2
end
What is Replace Method with Method object?
When we replace the long method (many params) with a class
We have
def calculate(price, amount) do_something(price, amount) do_something_else(price, amount) end
We translate to
class Calculate def initialize(price, amount) @price = price @amount = amount end
def call do_something do_something_else end
def do_something end
def do_something_else
end
end
def calculate(price, amount)
Calculate.new(price, amount).call
end
What is Move field and Move method technique?
When other class uses this method/field more often than yours.
What is Inline class technique?
When a class does almost nothing we can move his functional to another class and remove it
What is Extract class technique?
When your class does too much work which you can split it between several classes.
What is Remove middle man technique?
When a class does too much delegation works we can remove this delegation and use real methods.
What is Introduce local extension technique?
When you want to extend class with a new method but class is not available for modification, you can create a copy of this class with inheritance and add this method there.