Refactoring Flashcards

1
Q

What are the 3 most important steps for refactoring?

A

At the most abstract level, you’re looking for three things: complexity to break up, duplication to combine, and abstractions waiting to be born

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is code smell?

A

Code smells are warning signs about potential problems in code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is refactoring?

A

It’s changing code, without changing business logic in order to improve readability and the ability to update code easier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When we should avoid doing refactoring?

A
  • Project almost finished
  • This place won’t be changed in the future
  • Renaming of the open interface ( public API ) - Be careful
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Extract Method technique?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Inline method technique?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Inline Temp variable technique?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Replace Temp Variable with Query technique?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Introduce Explaining variable technique?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Replace Method with Method object?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Move field and Move method technique?

A

When other class uses this method/field more often than yours.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Inline class technique?

A

When a class does almost nothing we can move his functional to another class and remove it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Extract class technique?

A

When your class does too much work which you can split it between several classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Remove middle man technique?

A

When a class does too much delegation works we can remove this delegation and use real methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Introduce local extension technique?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is replace data value with object technique?

A

When you use simple data but it isn’t enough and you can create an object to handle it ( For example Money instead of number)

17
Q

What is replace type code with subclass technique?

A

When your class has many types with different (if/else) logic you can create a subclass for each of these types and move this logic there.

18
Q

What is replace conditional operator with polymorphism?

A

When you have a method with many switch/if conditions then you can create a subclass for each of these methods and move this logic there.