There's a powerful argument to create objects Immutable. So must we always pine for immutability where possible, or turn to the actual-world when determining whether something ought to be immutable?

Example: A card game. Is really a "Pack of cardsInch immutable? We've two options:

  1. Deck is immutable. All techniques (shuffle, deal-one) return a reference to a different deck object.
  2. Deck is mutable. All techniques mutate the present deck.

It might be nice to create when immutable. We'd gain the advantages referred to within the link above. However in the real life, decking is not immutable - we shuffle and obtain "exactly the sameInch pack of cards however in another order.

It is best to make an effort to get the best trade-offs for the scenario. There is no reason for 'pining' for any certain style, simply because it's in style, or perhaps is exactly what the awesome kids or doing. For those who have heard the arguments for and against, you need to have the ability to consider both options and making the decision that best suits you, your codebase, as well as your team.

Be skeptical of blindly following guidelines.

When it comes to this unique scenario using the pack of cards, I'd tend towards immutable automatically, but request myself a few pre-determined questions before I proceed:

  • can there be sufficient complexity between one occurrence of shuffling/dealing and subsequently? (If so, +1 for immutable, as it can certainly 'take a lot of the mind' as they say, when it comes to potential bugs that may be introduced should you share a reference)
  • may be the mention of deck shared across threads? (If so, +100 for immutable, because the complexity can greatly increase if you're able to have concurrent modifications)
  • can there be memory and/or speed constraints? (If so, +1 for any mutable deck, if you're truly resource restricted, decking modified in position is probably - although not guaranteed - to supply better performance, so that as always, be led with a profiler)
  • may be the in-memory deck needed to carefully map to some real-world deck? E.g. are the customers being able to access/querying it in their own individual programs/DSL? (if so, +1 for mutability, as it is most likely nearer to your user's mental model. But, that's still a definition you select, as well as an immutable deck can continue to seem sensible)

It is a weasel answer I am afraid, however, you just also have to evaluate based by yourself scenario. Sometimes creating to complement the domain is much more important than making your objects immutable. In other cases it isn't, and it is worth failing to remember the domain when you create a programming choice - remember your domain rarely needs to care or model several things we developers cope with on the day-to-day basis. In other situations along with other constraints you may have to quit both!

Immutability enables an item to become treated and seen just like a value. A card is a great candidate for immutability, because it does not really should be modified after.

Check this out article to assist decide when and where to make use of mutable/immutable classes IBM - Java theory and practice: To mutate or not to mutate?.