- source: https://code.tutsplus.com/courses/object-oriented-design-and-refactoring-patterns-in-ruby
- all refactoring patterns: http://refactoring.com/catalog/
- video lessons
Each item in the following list matches a code smell and contains a list of possible refactoring patterns to solve it.
-
Duplicate code
- Extract Method
- Pull Up Method
- Form Template Method
-
Long method
- Extract Method
- Replace Temp with Query / Replace Temp with Chain
- Introduce Parameter Object / Preserve Whole Object
- Replace Method with Method Object
-
Feature envy - «функциональная зависть», функціональна залежність
- Extract Method + Move Method
-
Data clumps
- Extract Class
- Introduce Parameter Object / Preserve Whole Object
-
Comments
- Rename Method
- Extract Method
- Introduce Assertion
-
Divergent change
- Extract Class
- Inline Class
- Move Method + Move Field
-
Primitive Obsession
- Replace Type Code with Polymorphism / Module Extension / State|Strategy
- Replace Array with Object
- Introduce Parameter Object
-
Conditionals (if/else, case/when)
- Introduce Null Object
- Replace Type Code with Polymorphism
- Extract Method + Move Method
-
Extract Class
- problem: You have one class doing work that should be done by two.
- solution: Create a new class and move the relevant fields and methods from the old class into the new class.
- example: http://refactoring.com/catalog/extractClass.html
-
Extract Method
- problem: You have a code fragment that can be grouped together.
- solution: Turn the fragment into a method whose name explains the purpose of the method.
- example: http://refactoring.com/catalog/extractMethod.html
-
Form Template Method
- problem: You have two methods in subclasses that perform similar steps in the same order, yet the steps are different.
- solution: Get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up.
- example: http://refactoring.com/catalog/formTemplateMethod.html
-
Hide Delegate
- problem: A client is calling a delegate class of an object.
- solution: Create methods on the server to hide the delegate.
- example: http://refactoring.com/catalog/hideDelegate.html
-
Introduce Assertion
- problem: A section of code assumes something about the state of the program.
- solution: Make the assumption explicit with an assertion.
- example: http://refactoring.com/catalog/introduceAssertion.html
-
Introduce Null Object
- problem: You have repeated checks for a null value.
- solution: Replace the null value with a null object.
- example: http://refactoring.com/catalog/introduceNullObject.html
-
Separate Query from Modifier
- problem: You have a method that returns a value but also changes the state of an object.
- solution: Create two methods, one for the query and one for the modification.
- example: http://refactoring.com/catalog/separateQueryFromModifier.html
-
Move Field
- problem: A field is, or will be, used by another class more than the class on which it is defined.
- solution: Create a new field in the target class, and change all its users.
- example: http://refactoring.com/catalog/moveField.html
-
Parameterize Method
- problem: Several methods do similar things but with different values contained in the method body.
- solution: Create one method that uses a parameter for the different values.
- example: http://refactoring.com/catalog/parameterizeMethod.html
-
Preserve Whole Object
- problem: You are getting several values from an object and passing these values as parameters in a method call.
- solution: Send the whole object instead.
- example: http://refactoring.com/catalog/preserveWholeObject.html
-
Pull Up Method
- problem: Two subclasses have the same field.
- solution: Move the field to the superclass.
- example: http://refactoring.com/catalog/pullUpField.html
-
Rename Method
- problem: The name of a method does not reveal its purpose.
- solution: Change the name of the method.
- example: http://refactoring.com/catalog/renameMethod.html
-
Replace Array with Object
- problem: You have an array in which certain elements mean different things.
- solution: Replace the array with an object that has a field for each element.
- example: http://refactoring.com/catalog/replaceArrayWithObject.html
-
Replace Method with Method Object
-
Replace Temp with Query
-
Replace Type Code with Module Extension
-
Replace Type Code with Polymorphism
-
Replace Type Code with State/Strategy