Refactorings Flashcards

1
Q

Define Rename Method/Field

A

Description: Method/field name doesn’t explain what it does

Smell: Comments

Fix: Rename the method

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

Define Extract Method

A

Description: Too long of methods or code needs comments to be understood

Smell: duplicate code, long method, feature envy, comments, switch statements, data class, message chains, data class

Fix: Take out related code from function and create a new method with it

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

Define Substitute Algorithm

A

Description: So you want to replace an existing algorithm with a new one?

Smell: duplicate code, long method

Fix: Replace the body of the method that implements the algorithm with a new algorithm.

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

Define Introduce Explaining Variable

A

Description: You have a really complex expression

Smell: ?

Fix: Create a local/temp variable to simplify expression and define what is happening

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

Define Replace Temp with Query

A

Description: You place the result of an expression (i.e a = b*c) in a local variable for later use in your code.

Smell: Long method, duplicate code

Fix: Move the entire expression to a separate method and return the result from it. Query the method instead of using a variable. Incorporate the new method in other methods, if necessary.

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

Define Replace Magic Number with Symbolic Constant

A

Description: Repeated use of a number to represent something (i.e 9.81 as gravity)

Smell: Comments

Fix: define final static variable to help make the number understandable

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

Define Add/Remove Parameter

A

Description: A method is missing data/A parameter isn’t used in the body of a method.

Smell: Speculative Generality(remove param)

Fix: Create copy of method but with necessary parameter/remove unused parameter from method.

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

Define Encapsulate Field

A

Description: You have a public variable

Smell: Data Class

Fix: Make it private and add getters and setters

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

Define Extract Class

A

Description: When one class does the work of two, awkwardness results.

Smell: Duplicate code, large class, divergent change, data clumps, primitive obsession, temporary fields, inappropriate intimacy

Fix: Instead, create a new class and place the fields and methods responsible for the relevant functionality in it.

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

Define Extract Superclass

A

Description: You have two classes with common fields and methods.

Smell: Duplicate code

Fix: Create a shared superclass for them and move all the identical fields and methods to it.

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

Define Move Method/Field

A

Description: A method/field is used more in another class than in its own class.

** rule of thumb is to put field in the class that has the most methods using it

Smell: Data class, shotgun surgery, message chains, inappropriate intimacy, switch statements, parallel inheritance hierarchy

Fix: Create a new method in the class that uses the method the most, then move code from the old method to there. Turn the code of the original method into a reference to the new method in the other class or else remove it entirely.

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

Define Pull up method/field

A

Description: Your subclasses have methods that perform similar work or duplicate fields.

Smell: duplicate code

Fix: Make the methods/fields identical and then move them to the relevant superclass.

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

Define Push down method/field

A

Description: method/field is only used in a few subclasses

Smell: Refused Bequest

Fix: push down method/field to subclasses that actually use it

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

Define Replace Constructor with Factory method

A

Description: You have a complex constructor that does something more than just setting parameter values in object fields.

Smell: ?

Fix: Create a factory (static) method and use it to replace constructor calls.

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

Define Form Template Method

A

Description: Your subclasses implement algorithms that contain similar steps in the same order.

Smell: Duplicate Code

Fix: Move the algorithm structure and identical steps to a superclass, and leave implementation of the different steps in the subclasses.

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

Define Collapse Hierarchy

A

Description: You have a class hierarchy in which a subclass is practically the same as its superclass.

Smell: Lazy class, speculative generality

Fix: Merge sub and superclass

17
Q

Define Remove Middle Man

A

Description: A class has too many methods that simply delegate to other objects. (i.e class1 calls class2 to call method from class3)

Smell: Middle man

Fix: Delete these methods and force the client to call the end methods directly. (delete original class1 methods and have class1 call class3 directly)

18
Q

Define Hide Delegate

A

Description: The client gets object B from a field or method of object А. Then the client calls a method of object B.

Smell: Message Chains, Inappropriate Intimacy

Fix: Create a new method in class A that delegates the call to object B. Now the client doesn’t know about, or depend on, class B.

19
Q

Define Remove Conditional with Polymorphism

A

Description: You have a conditional that performs various actions depending on object type or properties.

Smell: switch statements

Fix: Create subclasses matching the branches of the conditional. In them, create a shared method and move code from the corresponding branch of the conditional to it. Then replace the conditional with the relevant method call. The result is that the proper implementation will be attained via polymorphism depending on the object class.

20
Q

Define Introduce Parameter Object

A

Description: Your methods contain a repeating group of parameters.

Smell: Data clumps, long parameter list, long method, primitive obsession

Fix: Replace these parameters with an object.