If you’re a programmer, you might have heard of refactoring. Refactoring is the process of changing the internal structure of the code to make it easier to read and maintain. Moving methods is a form of refactoring, and it’s a great way to simplify your code.
Move Method refactoring allows you to move a method from one class to another. You move the code from one class to another without merging the classes or changing the observable behavior. The easiest way is to copy the method from the source class to the destination class and add missing dependencies.
This blog post is a great introduction to what move method refactoring is and how you can use it to simplify your code.
What is Move Method Refactoring?
In your job, you often have to read code written by others. You also need to modify the existing code to resolve some bugs. Sometimes, you can make these changes and be confident that they will be correct. Other times, you may need to modify the code, but you can’t be 100% sure that everything will be correct.
Move method can seem like the latter situation. But, if you do it right, this code refactoring can simplify your code.
Move Method is a refactoring process that moves a method to another class to isolate its behavior. Why do you need to do this? Sometimes you end up with a class with too many responsibilities, which results in code that’s difficult to maintain.
One way to solve this is to move some methods away from the current class.
How to do a Move Method refactoring
Moving a method from source to target class is done through several steps. You must follow these steps to ensure the method is refactored successfully.
- Inspect what dependencies and other class members the method uses. Decide if you need to move them as well to the new class.
- Check if a superclass or subclass reference this method. If they use it, maybe it won’t be possible to move the method.
- Create the method in the target class and copy the existing code to it. If the moved method uses an instance of the original class, you can pass it as a method parameter.
- Remove the code in the old method, and delegate the call to the new method.
- Optionally, decide whether to remove the old method and call the new method directly.
These are the steps you need to follow.
You might be wondering whether Visual Studio has a built-in tool for this refactoring? The answer is that it doesn’t have.
Move Method – example
Let’s take a look at the Person class and the method GetFullAddress. It should be moved to the AddressDetails class.
This is the same example used in the post about Extract Class refactoring, so check it out if you are interested in how did we get to this code.
class AddressDetails
{
internal string Street { get; set; }
internal int HouseNumber { get; set; }
internal string City { get; set; }
internal string PostalNumber { get; set; }
internal string Country { get; set; }
}
class Person
{
public Person()
{
Address = new AddressDetails();
}
public AddressDetails Address { get; set; }
internal string Name { get; set; }
internal string TelephoneNumber { get; set; }
internal string Email { get; set; }
internal string GetFullAddress()
{
return $"{Address.Street} {Address.HouseNumber}, {Address.PostalNumber} {Address.City}, {Address.Country}";
}
}
The first step is to define a new method with the same name in the AddressDetails class.
internal string GetFullAddress()
{
}
Next, you can copy the code from the GetFullAddress method of the Person class to it.
internal string GetFullAddress()
{
return $"{Street} {HouseNumber}, {PostalNumber} {City}, {Country}";
}
And finally, call the new method from the old method.
internal string GetFullAddress()
{
return Address.GetFullAddress();
}
Benefits and downsides of Move Method refactoring
There are many benefits of the Move Method refactoring. Some of these include:
- Better code organization
- Reduced repetition of code
- Easier maintenance of code
- Increased code readability and reusability
The downsides of Move Method refactoring are that it is a time-consuming process, and it can be difficult to identify the correct spot to refactor.
How to identify a situation that requires Move Method
One way to identify a situation when Move Method is needed is when you notice feature envy code smell.
What is Feature Envy? In software development, a “feature envy” is when a class wants to know the implementation detail of another class. One example can be if a method of one class uses too many members of another class. This was the case in the refactoring example you have seen above. GetFullAdress method uses five properties of the AddressDetails class.
Take a look one more time:
internal string GetFullAddress()
{
return $"{Address.Street} {Address.HouseNumber}, {Address.PostalNumber} {Address.City}, {Address.Country}";
}
If you are not familiar with the term “code smell,” this term generally refers to a symptom in the code that might indicate an underlying problem. A code smell can be roughly characterized as a difference between the way things should be and how things are.
Conclusion
Every developer knows that reducing the complexity of their code saves them time in the future, but some don’t know how to apply this knowledge. This is where Move Method refactoring comes into play. It helps you reduce the classes you have by having fewer methods.
Moving out methods simplifies the class and makes it easier to debug. I hope the information from this blog post had helped explain Move Method Refactoring in more detail. If you want to learn how to decrease your long methods, check out the Extract Method refactoring!