The Problem With Immutable Objects

Let’s find out about the problem we face with immutable objects in this lesson.

The costs associated with immutable objects

Although immutable objects solve a lot of problems with accidental modification of data as it is passed around in a program, it comes at a cost:

  • You may need to jump through a few hoops if you need to process the data.
  • You may end up making several copies of the data.

In many cases, it is worth using immutable data wherever possible for the sake of robustness. The main exception is for processing very large data structures. In that case, making a full copy of the data every time you make any change is just not practical. You are better off using mutable data, and simply taking extra care about when and where it is modified.

Get hands-on with 1200+ tech skills courses.