Paradigms in practice
How does functional programming compare with OOP?
On this page
A functional design often represents variation using data and functions over that data. An object-oriented design often assigns a common operation to several implementations. Adding a new shape or a new calculation stresses these arrangements differently. Many systems use pure calculations inside objects that own mutable resources.
Worked example
Pythonfrom dataclasses import dataclass
def discounted(cents, rebate):
return max(0, cents - rebate)
@dataclass(frozen=True)
class Voucher:
rebate: int
def apply(self, cents):
return discounted(cents, self.rebate)
assert Voucher(120).apply(100) == discounted(100, 120) == 0Explore class relationships in the OOP Playground