Disadvantages of immutable objects

I know that Immutable objects offer several advantages over mutable objects, since they are easier to reason than volatile, they do not have complex state spaces that change over time, we can move them freely, they make secure hash table keys, etc. d. So my question is what are the disadvantages of immutable objects?

+3
source share
4 answers

Quote from Effective Java :

The only real drawback of immutable classes is that they require a separate object for each individual value. Creating these objects can be expensive, especially if they are large. For example, suppose you are the millionth BigInteger, and you want to change its lower order bit:

BigInteger moby = ...; moby = moby.flipBit(0); 

The flipBit method creates a new instance of BigInteger , also having a length of a million bits, which differs from the original by only one bit. The operation takes time and space proportional to the size of BigInteger . Compare this to java.util.BitSet . Like BigInteger , a BitSet is an arbitrarily long sequence of bits, but unlike BigInteger , a BitSet mutable. The BitSet class provides a method for changing the state of one bit of a millionth instance in constant time.

Read the full article. Clause 15: Minimize Volatility

+6
source

In addition to possible performance flaws (possibly because with the complexity of GC and HotSpot optimizations, immutable structures are not necessarily slower) - one of the drawbacks may be that the state should now be threaded through your application. For simple applications or tiny scenarios, maintaining state in this way may be too high to buy you concurrency security.

For example, think of a graphical interface such as Swing. It would be entirely possible to write a GUI structure completely using immutable structures and one basic "unsafe" external loop, and I assume this was done in Haskell. Some problems of maintaining a nested immutable state can be considered, for example, using lenses. But managing all interactions (registering listeners, etc.) can be very attractive, so instead you want to introduce new abstractions, such as functional reactive or hybrid reactive graphical interfaces.

Basically, you lose some of the encapsulation of OO, going all immutable, and when that becomes a problem, there are alternative approaches like members or STM.

+3
source

I work with Scala daily. Confusion has some key benefits, as we all know. However, sometimes it’s just easier to allow volatile content in some situations. Here's a far-fetched example:

 var counter = 0 something.map {e => ... counter += 1 } 

Of course, I could just return the tuple map with the payload and counter, or use the collection.size file if one is available. But in this case, the volatile counter is perhaps clearer. In general, I prefer immutability, but also allow myself to make exceptions.

+1
source

To answer this question, I would like to quote Programming in Scala, second edition, chapter "Next Steps in Scala", paragraph 11, by Lex Lound, Bill Venners and Martin Odersky:

However, Scala's perspective is that val and var are just two different tools in your toolbox, useful and not inherent in evil. Scala encourages you to lean towards the vals, but ultimately achieve the best tool, given the task.

So, I would say that, just like for programming languages, val and var solve different problems: there is no "disavantage / avantage" without context, there is only a problem to solve, and both val / var address a different problem.

Hope this helps, even if it does not provide a specific list of pros and cons!

+1
source

All Articles