We've mentioned several times that immutability brings safety along with it. But that's not all; the following is a brief list of advantages that immutability brings with it, we will discuss them one by one:
- Thread safety
- Low coupling
- Referential transparency
- Failure atomicity
- Compiler optimization
- Pure functions
Let us now discuss each of the advantages to understand them better.
We have probably seen a thousand times that immutability brings thread safety to the table along with it. What does it actually mean and how does immutability achieve thread safety? Working with multiple threads is itself a complex job. When you are accessing a class from multiple threads, you need to ensure certain things, like locking and releasing of the object and synchronization, but none of them are required if you are accessing any immutable data from multiple threads.
Confused? Let's have an example with threads and mutable data:
class MyData { var someData:Int = 0...