Immutability refers to the property of an object or data structure that is can not be modified or changed after it is initialized. Once an object created, its state can not be altered, if there is a need for a change than new object created with those changes and first one will be stay unchanged. This is valuable design choise for data integrity concurreny in multi threaded applications. Immutable objects are useful in scenarios where consistency and predictability are crucial. In some applications you wouldnt want your same data be changed simultaneously by different parts of the application.
Why?
When a data leaves a source, it passes through many different systems, each system uses a point of this data and transfers it to another system. For example, a data coming out of the database gets the first shape on a programming language, then it is kept in the necessary "stacks" and "queues" on various "pipelines", then different "threads" use the data, various "message queues" send the data to "microservices" on servers running in different locations, they use something from the data and the data ends its journey at some point. But during this journey, many different pieces of code compete to process this data, and some access and process this data first, some later.
Thats where immutability shines. If this data is not immutable, we will be dealing with possible problems during the entire journey of the data. If the first code runs a few milliseconds late for very different reasons, and the second code handles that data before and deletes the required value, it is possible that we will encounter a problem at that moment.

» Read More »Labels: code, design patterns, software engineering