Our mutable example's design can produce unpredictable side-effects. If multiple clients hold a reference to the ProductTracker instance, there are two ways for the products to change from underneath any of these clients:
- We could simply reassign a value to the products directly. This is fixable by making it private for client calls but it is not fixable for in-class modifications.
- We could call addNewProduct() from any client and modify the products.
Either way, there are going to be side-effects and unintended consequences because of the mutation.
In our immutable example, it is impossible to cause those unintended consequences because our FunctionalProductTracker is a value type and all the properties ...