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

  • Michal@programming.dev
    link
    fedilink
    arrow-up
    9
    ·
    edit-2
    10 hours ago

    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!

    • sudo@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      10 hours ago

      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.