函式可以包裝行為,用命名解釋目的,消除重複程式碼。

Replace Inline Code with Function Call

# Before
x1, y1 = 1, 2
x2, y2 = 4, 6
distance = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
print(f"Distance between points: {distance}")

# After
def calculate_distance(x1, y1, x2, y2):
    return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5

distance = calculate_distance(1, 2, 4, 6)
print(f"Distance between points: {distance}")