2 ms·
Because JS doesn't clone natively and library behavior varies somewhat, our team has started to consider deep-copying and cloning to be a smell. It forces the m
by smt88 9y ago
Because JS doesn't clone natively and library behavior varies somewhat, our team has started to consider deep-copying and cloning to be a smell. It forces the maintainer to figure out how exactly the copying/cloning is going to work, and it also creates nasty bugs when modifying a class without knowing it'll be copied or cloned somewhere.
We instead only use immutable, plain ("POCO" or "POJO" in other languages) objects for data. If you want to copy an object, you have to call the same functions to change it that you called on the original. If the functions are deterministic (as they should be), then you get identical objects at the end.
Our code base is effectively functional, even though we still use objects as our syntactic sugar (addOwner(car, user) becomes car.addOwner(user), etc.)