Software entities should be open for extension, closed for modification. Adding a new case should not require editing existing code.
# dirty — adding a new shape requires editing solve
import math
def solve(kind, *dims):
if kind == 'circle':
return math.pi * dims[0] ** 2
if kind == 'rect':
return dims[0] * dims[1]
if kind == 'tri':
return 0.5 * dims[0] * dims[1]
raise ValueError('Unknown shape')Refactor so each shape has its own area callable. Create create_circle(r), create_rect(w, h), create_triangle(b, h). Then solve(kind, *dims) just builds the right shape and calls its area — no growing if chain needed when adding new shapes.
Sample tests