DEV Community

Discussion on: Setters on immutable objects?

Collapse
 
nickholmesde profile image
Nick Holmes

Formally, a setter is know as a mutator method. It's paradoxical to claim an immutable object has mutator methods. Setters do not formally return a value either. In many languages, you are not even able to return a value from a setter.

As this is a well understood concept, I would say its a bad practice to claim to follow it, but not.

That said, it's perfectly fine (and very common) to have methods on immutable classes that return new instances. This is the case with the String type in .Net, for example, and this is the source of many a bug;

myStr.ToUpper();

instead it should be

myUpperStr = myStr.ToUpper().
Collapse
 
bjornhollander profile image
BjornHollander

Agreed, I'm not apposed for methods to return new objects, it is simple the naming that got me thinking. :)