4

According to Effective Java's Item 15: Minimize Mutability, it is always better to make classes Immutable. According to that, how would I handle classes that are by nature given to change?

Some examples would be:

  • A Customer class, where an address or phone number can (and probably will) change.
  • A ShippingRule class, where shippers are selected based on different criteria. It makes sense that the rule should be editable...

In such cases, do we necessarily make them immutable? What other conventions are there?

  • 7
    You're conflating immutability with business rules. Just because your object is itself immutable doesn't mean the values can't change. It means that when they do change, it will be reflected in the code by creating a new object with the new value. The previous instance of the object will be discarded. – Vincent Savard Apr 7 '17 at 17:49
  • In that case, I'm afraid I may not have sufficiently understood immutable objects; can you please elaborate? How would you change a property value of an Immutable object? – IntelliData Apr 7 '17 at 17:53
  • Suppose you need previous copies of class instances, a history. Immutable instances might be very useful in such a situation. On the other hand, if your code becomes more complex, and you are not making it more reliable at the same time, you have to question the immutability. – Frank Hileman Apr 7 '17 at 18:01
  • 2
    "How would you change a property value of an Immutable object?" – You can't. That's the definition of Immutable Object. You can, however, create a new object that has a different value for that property. – Jörg W Mittag Apr 7 '17 at 19:30
  • 2
    If you have any familiarity with strings at all, I find them very instructive when trying to understand how other immutable classes would be used. Everything you can do to "modify" a string returns a new string. Most programmers do this on a daily basis without thinking much of it. – Doval Apr 8 '17 at 17:34
6

I read the chapter you stated, and I can see how you might be confused, as the chapter doesn't describe how you might use immutable objects.

Immutability is a core part of functional programming. To see how it works, consider this code:

MyImmutableClass newInstance = transform(MyImmutableClass originalInstance);

The transform method accepts the originalInstance object, transforms it (that transformation can be any operation), and returns a new object containing the transformation.

Why would you want to create a new object, instead of modifying an existing one?

  1. It creates a history of changes.
  2. It makes concurrency easier. Immutable objects are not subject to concurrency problems like race conditions. Functions using immutable objects can be easily decomposed into separate concurrent processes without problems.
  3. Immutable objects are easier to reason about.

In business applications involving CRUD operations, because you're typically already using a database with ACID guarantees, it's more common to simply mutate the values in the object you want to change, and then perform a Save (Update).

  • Immutability not only helps concurrency. In the single-threaded case, you are still sure that no methods you called between two points changed anything tacitly inside an immutable object, that an object passed as an argument to a method is guaranteed not to be changed inside it, no defensive copies needed, etc. – 9000 Apr 8 '17 at 5:46
  • @9000 So do you think all classes should be made immutable? And look at charlotte's answer, is that what should always be done? – IntelliData Apr 10 '17 at 13:41
  • @IntelliData: I see many benefits in immutable data. Still some things are easier with mutable data (e.g. a picture as a large bitmap, or a database connection that can open and close). I try to treat all data as immutable unless I see a compelling reason not to. – 9000 Apr 10 '17 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.