A method needs more information from its caller.
Add a parameter for an object that can pass on this information.
|
このページは大阪弁化フィルタによって翻訳生成されたんですわ。 |
10 December 2013
This catalog of refactorings includes those refactorings described in my original book on Refactoring, together with the Ruby Edition.
I've put together this catalog to help you find and explore the refactorings in the source books. You can expand each refactoring to show the problem/solution summary for the refactoring or you can navigate to the refactoring's online page. This page also contains the sketch for the refactoring and page references for the full description of the refactoring in the source book. There is also a deep link into the safari books online edition of the book, which is very handy if you've purchased that edition.
The filter panel at the left allows you to select the refactorings by keyword, and also by which books they appear in. You can save the current filter settings to a URL by using the "#" permalink at the bottom of the filter panel.
At this point, I only have refactorings from my original refactoring book and the ruby edition. I may add refactorings from other books later - this depends on my time and energy compared to other things I'd like to do.
select either all refactorings that appear in a book or just those refactorings that are only in a specific book
#
A method needs more information from its caller.
Add a parameter for an object that can pass on this information.
You have a two-way association but one class no longer needs features from the other.
Drop the unneeded end of the association.
You have a reference object that is small, immutable, and awkward to manage.
Turn it into a value object.
You have two classes that need to use each other's features, but there is only a one-way link.
Add back pointers, and change modifiers to update both sets.
You have a class with many equal instances that you want to replace with a single object.
Turn the object into a reference object.
You have a sequence of conditional tests with the same result.
Combine them into a single conditional expression and extract it.
The same fragment of code is in all branches of a conditional expression.
Move it outside of the expression.
You have a complicated conditional (if-then-else) statement.
Extract methods from the condition, then part, and else parts.
You have domain data available only in a GUI control, and domain methods need access.
Copy the data to a domain object. Set up an observer to synchronize the two pieces of data.
You have methods that can be defined more concisely if defined dynamically.
Define the methods dynamically.
Lazily initialization is causing more confusion than benefit
Initialize the attribute when you instantiate the object
A method returns a collection.
Make it return a read-only view and provide add/remove methods.
A method returns an object that needs to be downcasted by its callers.
Move the downcast to within the method.
You have one class doing work that should be done by two.
Create a new class and move the relevant fields and methods from the old class into the new class.
Several clients use the same subset of a class's interface, or two classes have part of their interfaces in common.
Extract the subset into an interface.
You have a code fragment that can be grouped together.
Turn the fragment into a method whose name explains the purpose of the method.
You have duplicated behavior in two or more classes.
You have duplicated behavior in two or more classes.
A class has features that are used only in some instances.
Create a subclass for that subset of features.
You have two classes with similar features.
Create a superclass and move the common features to the superclass.
You have two methods that contain nearly identical code. The variance is in the middle of the method.
Extract the duplication into a method that accepts a block and yields back to the caller to execute the unique code.
You have a complicated expression.
Put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose.
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.
A client is calling a delegate class of an object.
Create methods on the server to hide the delegate.
A method's body is just as clear as its name.
Put the method's body into the body of its callers and remove the method.
The resultant indirection of the included module is no longer worth the duplica- tion it is preventing.
Merge the module into the including class.
You have a temp that is assigned to once with a simple expression, and the temp is getting in the way of other refactorings.
Replace all references to that temp with the expression.
A section of code assumes something about the state of the program.
Make the assumption explicit with an assertion.
You have a method whose implementation steps are so common that they can safely be hidden away.
Declare the behavior by calling a class method from the class definition.
You want to interact with a public interface in a more fluent manner and not muddy the interface of an existing object.
Introduce an Expression Builder and create an interface specific to your application.
A server class you are using needs an additional method, but you can't modify the class.
Create a method in the client class with an instance of the server class as its first argument.
You want to interact with a complex API of an external system or resource in a simplified way
Introduce a Gateway that encapsulates access to an external system or resource
A server class you are using needs several additional methods, but you can't modify the class.
Create a new class that contains these extra methods. Make this extension class a subclass or a wrapper of the original.
The parameters in a method call cannot easily be deduced from the name of the method you are calling.
Convert the parameter list into a Hash, and use the keys of the Hash as names for the parameters.
A class utilizing method_missing has become painful to alter.
Introduce a new class and move the method_missing logic to that class.
An attribute takes time to initialize but is only accessed rarely
Initialize when it's first used
You need to use eval but want to limit the number of times eval is necessary.
Move the use of eval from within the method definition to defining the method itself.
A field is, or will be, used by another class more than the class on which it is defined.
Create a new field in the target class, and change all its users.
A method is, or will be, using or used by more features of another class than the class on which it is defined.
Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether.
Several methods do similar things but with different values contained in the method body.
Create one method that uses a parameter for the different values.
You are getting several values from an object and passing these values as parameters in a method call.
Send the whole object instead.
You have constructors on subclasses with mostly identical bodies.
Create a superclass constructor; call this from the subclass methods.
Behavior on a superclass is relevant only for some of its subclasses.
Move it to those subclasses.
You have conditional code that is unnecessarily verbose and does not use the most readable Ruby construct.
Replace the conditional code with the more idiomatic Ruby construct.
You have a variable that is acting as a control flag for a series of boolean expressions.
Use a break or return instead.
The fluency that the named parameter brings is no longer worth the complexity on the receiver.
Convert the named parameter Hash to a standard parameter list.
A field should be set at creation time and never altered.
Remove any setting method for that field.
A parameter has a default value, but the method is never called without the parameter.
Remove the default value
You have an inheritance hierarchy, but never intend to explicitly instantiate an instance of the superclass.
Replace the superclass with a module to better communicate your intention.
You have an array in which certain elements mean different things.
Replace the array with an object that has a field for each element.
You have a conditional that chooses different behavior depending on the type of an object.
Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract.
You want to do more than simple construction when you create an object.
Replace the constructor with a factory method.
You have a data item that needs additional data or behavior.
Turn the data item into an object.
You’re using delegation and are often writing many simple delegations for the entire interface
Make the delegate a module and include it into the delegating class.
You're using delegation and are often writing many simple delegations for the entire interface.
Make the delegating class a subclass of the delegate.
You have methods you want to handle dynamically without the pain of debug- gingmethod_missing.
Use dynamic method definition to define the necessary methods.
You are throwing an exception on a condition the caller could have checked first.
Change the caller to make the test first.
You have a hash that stores several different types of objects, and is passed around and used for more than one purpose.
Replace the hash with an object that has a field for each key
A subclass uses only part of a superclasses interface or does not want to inherit data.
Create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing.
You are processing the elements of a collection in a loop.
Replace the loop with a collection closure method.
You have a literal number with a particular meaning.
Create a constant, name it after the meaning, and replace the number with it.
You have a long method that uses local variables in such a way that you cannot apply
Turn the method into its own object so that all the local variables become fields on that object. You can then decompose the method into other methods on the same object.
A method has conditional behavior that does not make clear what the normal path of execution is
Use Guard Clauses for all the special cases
You have a method that runs different code depending on the values of an enumerated parameter.
Create a separate method for each value of the parameter.
An object invokes a method, then passes the result as a parameter for a method. The receiver can also invoke this method.
Remove the parameter and let the receiver invoke the method.
You need to interface with a record structure in a traditional programming environment.
Make a dumb data object for the record.
You have subclasses that vary only in methods that return constant data.
Change the methods to superclass fields and eliminate the subclasses.
You are using a temporary variable to hold the result of an expression
Change the methods to support chaining, thus removing the need for a temp.
You are using a temporary variable to hold the result of an expression.
Extract the expression into a method. Replace all references to the temp with the expression. The new method can then be used in other methods.
You have a type code that affects the behavior of a class.
Replace the type code with classes: one for each type code variant.
A class has a numeric type code that does not affect its behavior.
Replace the number with a new class.
You have a type code that affects the behavior of a class.
Replace the type code with dynamic module extension.
You have a type code that affects the behavior of a class, but you cannot use subclassing.
Replace the type code with a state object.
You have an immutable type code that affects the behavior of a class.
Replace the type code with subclasses.
You are accessing a field directly, but the coupling to the field is becoming awkward.
Create getting and setting methods for the field and use only those to access the field.
You have a method that returns a value but also changes the state of an object.
Create two methods, one for the query and one for the modification.
You have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable.
Make a separate temporary variable for each assignment.
You want to replace an algorithm with one that is clearer.
Replace the body of the method with the new algorithm.