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

  • tunetardis@lemmy.ca
    link
    fedilink
    English
    arrow-up
    1
    ·
    18 days ago

    Well, but then you’re basically just pushing the mutability onto the container, since you need to be able to replace elements within it.

    It’s a good strategy at times though. Like say you’re working in a language where strings are immutable and you want a string you can change. You can wrap it in a list along the lines s=['foo'] and pass references to the list around instead. Then if you go s[0]='bar' at some point, all the references will now see ['bar'] instead.