Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is immutability useful on non parallel applications?

I like the immutability concept but sometimes I wonder, when an application isn't meant to be parallel, should one avoid making things immutable?

When an application isn't multi-threaded, you aren't plagued by shared state problems, right?

Or is immutability a concept like OOP that you either use all the way or not? Excluding the cases when something shouldn't be immutable based on use/performance, etc.

I am faced with this question when writing an application for myself, that is moderately big (maybe like 1-2k lines).

like image 551
Joan Venge Avatar asked Jun 22 '09 21:06

Joan Venge


People also ask

Why would you use immutability?

Immutability allows you to track the changes that happen to these objects like a chain of events. Variables have new references that are easy to track compared to existing variables. This helps in debugging the code and building the concurrent application.

Can we use immutable objects in multi-threaded programs?

Actually immutable objects are always thread-safe, but its references may not be. Going back to basic: Thread-safe simply means that two or more threads must work in coordination on the shared resource or object. They shouldn't over-ride the changes done by any other thread.

What is object immutability Why is it preferred?

Immutable objects are good Map keys and Set elements, since these typically do not change once created. Immutability makes it easier to write, use and reason about the code (class invariant is established once and then unchanged).

Why is immutability so important in a multi-threaded Java application?

Immutability is a powerful, language-agnostic concept, and it's fairly easy to achieve in Java. To put it simply, a class instance is immutable when its internal state can't be modified after it has been constructed. A MessageService object is effectively immutable since its state can't change after its construction.


2 Answers

I like the advantage from immutability that you need to validate it only once - at creation of object. That's a huge bonus actually.

like image 165
Arnis Lapsa Avatar answered Sep 20 '22 15:09

Arnis Lapsa


I love immutability because it means I don't have to trust other peoples code not to mess around with objects I expect to stay the same.

When you pass an object off to another component such as a List<T>, you are at the mercy of what that component does. This is especially important when you return collections as properties.

public class Foo { 
  private List<Bar> _barList;
  public ICollection<Bar> BarList { get return _barList; } 
}

There's nothing stopping a consumer of this class from clearing the collection out from under me. Even switching the return type to IEnumerable<Bar> is not entirely safe. There's nothing stopping some piece of badly written code from casting this back to List<T> and calling .Clear().

However if I really want the collection to stay consistent I could rewrite it as followis

public class Foo {
  private ImmutableCollection<Bar> _barList;
  public ImmutableCollection<Bar> BarList { get { return _barList; } }
}

Now I'm safe from having to trust other code from using my class incorrectly. They can't mess it up.

like image 21
JaredPar Avatar answered Sep 20 '22 15:09

JaredPar