Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros. / Cons. of Immutability vs. Mutability

Well, I guess it's clear the direction from which I am trying to reason about this. There is a lot of talk these days about the virtues of immutability (constness) whenever possible. The Concurrent programming in Java book talks a lot about this too.

However, all this is just what I've read about. I personally, haven't coded much in Functional languages. And it looks very surprising to me that it is possible to work comfortably with immutable objects. Theoretically, it is definitely possible. But, from a practical point of view, is a very comfortable experience. Or what is the new kind of reasoning (for FP) that I must develop so that I don't need mutability so much.

I would appreciate how to think about writing programs when you are forced to use immutable objects.

  • Already making this a Community Wiki ... for all those who are interested in closing this question or marking this as subjective ... etc. etc. ... *

1 Answers

Immutability has several advantages, including (but not limited to):

  • Programs with immutable objects are less complicated to think about, since you don't need to worry about how an object may evolve over time.
  • You don't need to make defensive copies of immutable objects when returning or passing to other functions, since there is no possibility an immutable object will be modified behind your back.
  • One copy of an object is just as good as another, so you can cache objects or re-use the same object multiple times.
  • Immutable objects are good for sharing information between threads in a multi-threaded environment since they don't need to be synchronized.
  • Operations on immutable objects return new immutable objects while operations that cause side-effects on mutable objects usually return void. This means several operations can be chained together. For instance

("foo" + "bar" + "baz").length()

  • In languages where functions are first class values, operations like map, reduce, filter, etc. are basic operations on collections. These can be combined in many ways, and can replace most loops in a program.

There are of course some disadvantages:

  • Cyclic data structures such as graphs are difficult to build. If you have two objects which can't be modified after initialization, how can you get them to point to each other?
  • Allocating lots and lots of small objects rather than modifying ones you already have can have a performance impact. Usually the complexity of either the allocator or the garbage collector depends on the number of objects on the heap.
  • Naive implementations of immutable data structures can result in extremely poor performance. For instance, concatenating many immutable strings (like in Java) is O(n2) when the best algorithm is O(n). It is possible to write efficient immutable data structures, it just takes a little more thought.
like image 120
Jay Conrod Avatar answered Sep 11 '25 03:09

Jay Conrod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!