DEV Community

Discussion on: The {... } is dangerous

 
ahmedgaafer profile image
ahmedgaafer

thank you so much <3 this was an amazing explanation

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

I enjoyed the reading, just want to break a spear in favour of mutation.

const setName = ( person, name ) => { person.name = name; }
const person = { 
  name: ''
};
setName( person, "fred" );
console.log(person)
Enter fullscreen mode Exit fullscreen mode

Using proper names makes it pretty understandable. Even that, sure it's better to create new instances to avoid bugs.

A usecase that usually takes place when this situation happens is an object being used in different places and a dev adding a mutation in the middle just for a new use case, breaking things in other data paths (use-cases). If you instantiate a new object, array or whatever and you work with your own instance this will hardly take place.

There are other methods to avoid this, of course. Interfaces in OOP, and good practices when using FP, maybe the most important thing is to differentiate well whether are you defining imperative statements and were and how you use declarative ones.

Thread Thread
 
webbureaucrat profile image
webbureaucrat

Using proper names makes it pretty understandable.

I agree... but that's largely because I intentionally made the example trivial so that the issue would be easy to understand. Real-world mutation bugs can be extremely subtle.

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

Also agree with that