It makes the code icky and hard to debug, and you can simply return new immutable objects for every state change.
EDIT: why not just create a new object and reassign variable to point to the new object
It makes the code icky and hard to debug, and you can simply return new immutable objects for every state change.
EDIT: why not just create a new object and reassign variable to point to the new object
Because recreating entire object just to make a single change is dumb.
God help you if you’ve already passed the object by reference and have to chase up all the references to point at the new version!
You can safely do a shallow copy and re-use references to the unchanged members if you have some guarantee that those members are also immutable. Its called Persistent Data Structures. But that’s a feature of the language and usually necessitates a GC.