I am wondering as to why Date structures & objects, like C#’s DateTime & Obj-C’s NSDate have been made immutable.
Im looking for the reasoning behind this design and the benefits of making this information immutable, not just “because they can”
UPDATE:
It seems as though there was a similar question with a fantastic answer to mine but specifically targeting Java, it can be found here: Why do we need immutable class?
That in combination with the answer to my question has been very informative
Immutability makes a lot of things easier. If you don’t have to worry that something can change underneath you, then there is a lot of guard code you don’t have to write. There’s a lot of assumptions you don’t have to make. There’s a lot of possibilities for things to NOT go wrong.
It’s common for strings, dates, and other kinds of common objects to be immutable in modern languages because it simplifies the compiler, the framework, and means the compiler is free to make assumptions when doing optimization.
The gist is that, for a small price (having to create a new object if you want to change the value) you get a lot of real performance, stability, and reliability.