Do you have a method that returns a value but also changes something inside an object?
Solution
Split the method into two separate methods. As you would expect, one of them should return the value and the other one modifies the object.
Before
After
Why Refactor
This factoring technique implementsCommand and Query Responsibility Segregation. This principle tells us to separate code responsible for getting data from code that changes something inside an object.
Code for getting data is named aquery. Code for changing things in thevisible state of an object is named amodifier. When aquery andmodifier are combined, you do not have a way to get data without making changes to its condition. In other words, you ask a question and can change the answer even as it is being received. This problem becomes even more severe when the person calling the query may not know about the method’s “side effects”, which often leads to runtime errors.
But remember that side effects are dangerous only in the case ofmodifiers that change thevisible state of an object. These could be, for example, fields accessible from an object’s public interface, entry in a database, in files, etc. If amodifier only caches a complex operation and saves it within the private field of a class, it can hardly cause any side effects.
Benefits
If you have aquery that does not change the state of your program, you can call it as many times as you like without having to worry about unintended changes in the result caused by the mere fact of you calling the method.
Drawbacks
In some cases it is convenient to get data after performing a command. For example, when deleting something from a database you want to know how many rows were deleted.
How to Refactor
Create a newquery method to return what the original method did.
Change the original method so that it returns only the result of calling the newquery method.
Replace all references to the original method with a call to thequery method. Immediately before this line, place a call to themodifier method. This will save you from side effects in case if the original method was used in a condition of a conditional operator or loop.
Get rid of the value-returning code in the original method, which now has become a propermodifier method.
Reading is boring
Aren't you bored of reading so much? Try out our new interactive learning course on refactoring. It has more content and much more fun.