The Tech World

Move Method | Drawbacks and Resolving Strategy

A method is transferred from one class to another using the refactoring technique known as the “Move Method.” This method is useful when a method appears to be more closely related to another class than it is to its own or when the method uses more features from another class than it does from its own.

Ways to Apply Move Method Refactoring Technique

The Move Method refactoring technique involves the following steps:

Problem

A method is utilized more frequently in another class than in the one it belongs to.

Before Refactoring

class Order:
    def __init__(self, items):
        self.items = items
    
    def calculate_total(self):
        total = 0
        for item in self.items:
            total += item.price
        return total
    
    def print_order(self):
        print("Order Details:")
        for item in self.items:
            print(f"- {item.name}: {item.price}")

class Item:
    def __init__(self, name, price):
        self.name = name
        self.price = price

item1 = Item("Item 1", 10)
item2 = Item("Item 2", 20)
item3 = Item("Item 3", 15)

order = Order([item1, item2, item3])
order.print_order()
print("Total:", order.calculate_total())

Solution

The most frequently used method should be moved to a new method that is created in the class. A reference to the new method in the other class should be added to the original method’s code, or the original method itself should be deleted.

After Refactoring

class Order:
    def __init__(self, items):
        self.items = items
    
    def calculate_total(self):
        total = 0
        for item in self.items:
            total += item.price
        return total

class Item:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def print_details(self):
        print(f"- {self.name}: {self.price}")

class Printer:
    def print_order(self, items):
        print("Order Details:")
        for item in items:
            item.print_details()

item1 = Item("Item 1", 10)
item2 = Item("Item 2", 20)
item3 = Item("Item 3", 15)

printer = Printer()
printer.print_order([item1, item2, item3])

order = Order([item1, item2, item3])
print("Total:", order.calculate_total())

The print_order() method was transferred from the Order class to a new Printer class in the refactored version. The Order class is in charge of managing the items and calculating the total, while the Printer class is in charge of printing the order details.

With the help of the Move Method refactoring technique, we have increased the modularity and maintainability of the code by reducing the responsibility of the Order class and improving the separation of concerns.

Drawbacks of the Move Method Refactoring Technique

The Move Method refactoring technique has various disadvantages that should be taken into account, despite the fact that it is frequently advantageous:

Resolving Strategy

When using the Move Method refactoring technique, it is critical to have a resolution strategy in place to resolve potential issues and minimize the impact on the codebase. Here are some techniques for mitigating the disadvantages:

Yocan also views other refactoring techniques:

Exit mobile version