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
function solve(type, ...dims) {
if (type === 'circle') return Math.PI * dims[0] ** 2;
if (type === 'rect') return dims[0] * dims[1];
if (type === 'tri') return 0.5 * dims[0] * dims[1];
throw new Error('Unknown shape');
}Refactor so each shape has its own area() method. Create createCircle(r), createRect(w, h), createTriangle(b, h). Then solve(type, ...dims) just instantiates the right shape and calls .area() — no if chains needed when adding new shapes.
Sample tests