munch2024 / munch

2 stars 11 forks source link

Code Refactoring Issue #52

Closed haiimkeith closed 8 months ago

haiimkeith commented 8 months ago

I've identified a code snippet in an old python project that calculates Euclidean and Manhattan distances between two points. However, the current implementation involves duplicated code for both distance calculation methods, which can lead to maintenance issues and reduced readability.

The code includes separate functions for calculating Euclidean and Manhattan distances that could be jointed by one function, with each function containing duplicated logic when performing distance calculations each step.

Goals for Refactoring:

  1. Refactoring the code by creating one combine function for both Manhattan and Euclidean distance calculations.
  2. Improve code readability by reworking duplication distance logic.
  3. Ensure efficient distance calculation across the project that supports the workflow for the rest of the project.

Below is the attached code snippet of the issue.

Code Snippet:

import math

def manhattan_distance(point1, point2):
    delta_x = abs(point1[0] - point2[0])
    delta_y = abs(point1[1] - point2[1])
    return delta_x + delta_y

def euclidean_distance(point1, point2):
    delta_x = point1[0] - point2[0]
    delta_y = point1[1] - point2[1]
    squared_distance = delta_x**2 + delta_y**2
    return math.sqrt(squared_distance)

# Example usage
point_a = (3, 4)
point_b = (6, 8)

manhattan_dist = manhattan_distance(point_a, point_b)
euclidean_dist = euclidean_distance(point_a, point_b)

print("Manhattan Distance:", manhattan_dist)
print("Euclidean Distance:", euclidean_dist)