this post was submitted on 12 Feb 2025
42 points (92.0% liked)

Programming

18169 readers
396 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] Michal@programming.dev 7 points 5 hours ago* (last edited 5 hours ago) (1 children)

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 2 points 4 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.