What is immutability and why should I worry about it?

What is Immutability?

  • Immutability is applied primarily to objects (strings, arrays, a custom Animal class)
  • Typically, if there is an immutable version of a class, a mutable version is also available. For instance, Objective-C and Cocoa define both an NSString class (immutable) and an NSMutableString class.
  • If an object is immutable, it can’t be changed after it is created (basically read-only). You could think of it as “only the constructor can change the object”.

This doesn’t directly have anything to do with user input; not even your code can change the value of an immutable object. However, you can always create a new immutable object to replace it. Here’s a pseudocode example; note that in many languages you can simply do myString = "hello"; instead of using a constructor as I did below, but I included it for clarity:

String myString = new ImmutableString("hello");
myString.appendString(" world"); // Can't do this
myString.setValue("hello world"); // Can't do this
myString = new ImmutableString("hello world"); // OK

You mention “an object that just returns information”; this doesn’t automatically make it a good candidate for immutability. Immutable objects tend to always return the same value that they were constructed with, so I’m inclined to say the current time wouldn’t be ideal since that changes often. However, you could have a MomentOfTime class that is created with a specific timestamp and always returns that one timestamp in the future.

Benefits of Immutabilty

  • If you pass an object to another function/method, you shouldn’t have to worry about whether that object will have the same value after the function returns. For instance:

    String myString = "HeLLo WoRLd";
    String lowercasedString = lowercase(myString);
    print myString + " was converted to " + lowercasedString;
    

    What if the implementation of lowercase() changed myString as it was creating a lowercase version? The third line wouldn’t give you the result you wanted. Of course, a good lowercase() function wouldn’t do this, but you’re guaranteed this fact if myString is immutable. As such, immutable objects can help enforce good object-oriented programming practices.

  • It’s easier to make an immutable object thread-safe

  • It potentially simplifies the implementation of the class (nice if you’re the one writing the class)

State

If you were to take all of an object’s instance variables and write down their values on paper, that would be the state of that object at that given moment. The state of the program is the state of all its objects at a given moment. State changes rapidly over time; a program needs to change state in order to continue running.

Immutable objects, however, have fixed state over time. Once created, the state of an immutable object doesn’t change although the state of the program as a whole might. This makes it easier to keep track of what is happening (and see other benefits above).

Leave a Comment