![code refactoring](https://res.cloudinary.com/practicaldev/image/fetch/s--HTHvR0qi--/c_imagga_scale,f_auto,fl_progressive,h_500,q_auto,w_1000/https://dev-to-uploads.s3.amazonaws.com/i/o5cv4dwwcqlgpdkhpsky.jpg) Code refactoring is the process of modifying software code to improve its internal structure without changing its external behavior. Below, I present an example of a Python code that goes through several stages of refactoring, using common techniques such as renaming variables, extracting methods and introducing design patterns. # <br>Original code Let's start with some simple, functional code that could be improved: ```py def calc_area_and_perimeter(shape, dimension1, dimension2=None): if shape == 'rectangle': if dimension2 is None: return "For a rectangle, both dimensions are required" area = dimension1 * dimension2 perimeter = 2 * (dimension1 + dimension2) elif shape == 'circle': if dimension2 is not None: return "For a circle, only one dimension is required" import math area = math.pi * (dimension1 ** 2) perimeter = 2 * math.pi * dimension1 else: return "Unknown shape" return area, perimeter # Example of use print(calc_area_and_perimeter('rectangle', 4, 5)) print(calc_area_and_perimeter('circle', 3)) print(calc_area_and_perimeter('triangle', 3, 4)) ``` # <br>Step 1: Renaming Variables Let's rename variables to make the code more readable and intuitive. ```py def calculate_area_and_perimeter(shape, dimension1, dimension2=None): if shape == 'rectangle': if dimension2 is None: return "For a rectangle, both length and width are required" area = dimension1 * dimension2 perimeter = 2 * (dimension1 + dimension2) elif shape == 'circle': if dimension2 is not None: return "For a circle, only the radius is required" import math area = math.pi * (dimension1 ** 2) perimeter = 2 * math.pi * dimension1 else: return "Unknown shape" return area, perimeter # Example of use print(calculate_area_and_perimeter('rectangle', 4, 5)) print(calculate_area_and_perimeter('circle', 3)) print(calculate_area_and_perimeter('triangle', 3, 4)) ``` # <br>Step 2: Extracting methods Let's extract methods to calculate the area and perimeter of each geometric shape. ```py import math def calculate_rectangle_area_and_perimeter(length, width): area = length * width perimeter = 2 * (length + width) return area, perimeter def calculate_circle_area_and_perimeter(radius): area = math.pi * (radius ** 2) perimeter = 2 * math.pi * radius return area, perimeter def calculate_area_and_perimeter(shape, dimension1, dimension2=None): if shape == 'rectangle': if dimension2 is None: return "For a rectangle, both length and width are required" return calculate_rectangle_area_and_perimeter(dimension1, dimension2) elif shape == 'circle': if dimension2 is not None: return "For a circle, only the radius is required" return calculate_circle_area_and_perimeter(dimension1) else: return "Unknown shape" # Example of use print(calculate_area_and_perimeter('rectangle', 4, 5)) print(calculate_area_and_perimeter('circle', 3)) print(calculate_area_and_perimeter('triangle', 3, 4)) ``` # <br>Step 3: Introducing Design Patterns We can use the Factory Method design pattern to create geometric shape objects. This makes the code more extensible for adding new shapes in the future. ```py import math class Shape: def calculate_area_and_perimeter(self): pass class Rectangle(Shape): def __init__(self, length, width): self.length = length self.width = width def calculate_area_and_perimeter(self): area = self.length * self.width perimeter = 2 * (self.length + self.width) return area, perimeter class Circle(Shape): def __init__(self, radius): self.radius = radius def calculate_area_and_perimeter(self): area = math.pi * (self.radius ** 2) perimeter = 2 * math.pi * self.radius return area, perimeter def shape_factory(shape, dimension1, dimension2=None): if shape == 'rectangle': if dimension2 is None: return "For a rectangle, both length and width are required" return Rectangle(dimension1, dimension2) elif shape == 'circle': if dimension2 is not None: return "For a circle, only the radius is required" return Circle(dimension1) else: return "Unknown shape" # Example of use rectangle = shape_factory('rectangle', 4, 5) circle = shape_factory('circle', 3) triangle = shape_factory('triangle', 3, 4) print(rectangle.calculate_area_and_perimeter() if isinstance(rectangle, Shape) else rectangle) print(circle.calculate_area_and_perimeter() if isinstance(circle, Shape) else circle) print(triangle.calculate_area_and_perimeter() if isinstance(triangle, Shape) else triangle) ``` ## Conclusion Refactoring improves the readability, reusability and maintainability of code. By applying these techniques, we can create a more robust and extensible code base. The example above illustrates the transformation of an initial, functional code to a more organized and scalable version.