Side effects and mutability
When something is immutable, it means its state can’t be changed once it’s created. Understanding why immutability is a good thing involves recognizing the problems that can arise from its opposite—mutability.
There are several downsides to having mutable objects or, in general, in being forced to handle mutability (and thus, handling objects’ state).
A mutable class that has setup functions can easily be misconfigured, leading to an invalid state; this is a sort of addendum (a spin-off) to what we said before (“good code is hard to misuse”). If you allow a class to have some kind of really detailed and complicated setup functions, it will be more likely to misuse it, ending up with a wrong or invalid configuration that will end in some malfunctioning in your code.
Another big issue with mutable objects arises when you think about thread safety, a critical concern in multi-threaded or parallel programming...