Last Revision — April 19, 2022
2 Min Read
---
Functional Abusers
Data
Between Classes
- Mutable Data Class (family)
- Side Effects (causes)
Martin Fowler in book (2018): "Refactoring: Improving the Design of Existing Code (3rd Edition)"
Mutable data are harmful because they can unexpectedly fail other parts of the code. It is a rich source of bugs that are hard to spot because they can occur under rare conditions. Fowler says that this is a significant factor which contributed to the rise of the new programming school, functional programming, in which one of the principles is that the data should never change. While these languages are still relatively small, he states that developers should not ignore the advantages of immutable data [1]. It is hard to reason about these variables, especially when they have several reassignments.
In Object-Oriented Programming, the immutability of objects was not addressed audibly, if at all, in the context of something desirable. This is a relatively new concept.
Mutable Data is a rich source of hidden bugs that might be spotted only when specific rare conditions are met.
Data that might change at any moment is hard to reason about without excluding every corner case.
@dataclass
class Foo:
name: str
value: float
premium: bool
# foo object instance will be passed around and modified
@dataclass(frozen=True)
class Foo:
name: str
value: float
premium: bool
# foo object instance will be passed around and, if that is necessary,
# recreated, thus making the state change explicit for everyone and handled