Setters on immutable objects?

bjornhollander

BjornHollander

Posted on December 18, 2019

Setters on immutable objects?

Hello everyone! Recently I encountered a class which was defined to produce immutable objects. It concerned a class which encompassed a time period, with a start date and an optional end date. However to my surprise I discovered the class also had a few setter methods.
Now I have to acknowledge that the setter methods did return a new instance and left the old object in tact (as is should!), but still something inside me found it strange to define these methods as setters. If someone by accident misses the immutable character of the class this could quickly lead to strange behaviour in the application.

Basically my question is, how do you feel about setter methods on a class defined to produce immutable objects?

💖 💪 🙅 🚩
bjornhollander
BjornHollander

Posted on December 18, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Setters on immutable objects?
discuss Setters on immutable objects?

December 18, 2019