Item 15: Minimize Mutability

Confused what Item 15 is? Well, as I have already mentioned in my previous posts, ‘Items’ are 78 items mentioned in Joshua Bloch’s book ‘Effective Java’. Those, well versed with the items in Effective Java may read them as a reminder, rest can have fun getting to know things.

Moving to the next item, minimizing mutability. But why reduce mutability, when mutability can mean giving control to someone who will be using your API. Well, with great power comes great responsibility. The more control you give your client, the more damage can be caused to the client, and less flexible becomes your API.

How can I make a class immutable?

  1. Don’t provide any methods that modify the object’s state. These methods are known as mutators.
  2. Ensure that the class cannot be extended. You can ensure this by making the class final.
  3. Make as much fields as you can, final.
  4. Make all the fields private.
  5.  Make accessor methods only for fields which need to be made available.
    Here, lies a big decision in programming. Immutable objects are considered best friends for those dealing with threading environments. While for those, having to deal with the speed and gui changes, change of state is much cheaper action. So, don’t forget that constructor is a heavy operation if you are going for immutable objects. And also, keep in mind that change of states may result in chaos in threaded environments.
  6. Exclusive access for safety in threads. Providing an exclusive access is not letting another thread get the reference to the mutable object. Lets say we deal in money, and we have a field called balance,which should not be altered by any other thread. So, to keep things safe and simple, we write the add method like this-
    public Money add(BigDecimal money) {
        return new Money(this.amount.add(money));
    }
    
  7. Sometimes, a class has a set of frequently used instances. Don’t forget to create final static instances for the frequently used instances of the target class. e.g. for complex numbers, some frequently used numbers are –
    public static final Complex ZERO = new Complex(0, 0);
    public static final Complex ONE = new Complex(1, 0);
    public static final Complex I = new Complex(0, 1);
    

Providing Mutable Companion

What’s a mutable companion? String has a mutable companion called StringBuilder. The moral of the story is that if a class cannot be made immutable, limit its mutability as much as possible. e.g. lets make money immutable and provide an mutable companion.


Are there any disadvantages of immutability?

The only real disadvantage of immutable classes is that they require a separate object for each distinct value. Creating these objects can be costly, especially if they are large.
Advertisements

About Gaurav

I'm Gaurav; friends call me Teddy, n i'm shivi fr my family!! A java enthusiast who likes experimenting with what he knows. View all posts by Gaurav

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: