3 ms·
Cloning is super useful and not a code smell at all in several scenarios. My most common use is for delta detection and json-patch based APIs. I keep the orig
by claytongulick 4y ago
Cloning is super useful and not a code smell at all in several scenarios.
My most common use is for delta detection and json-patch based APIs.
I keep the original entity and allow the user to modify a clone.
When it's time to sync, I compare it to the original to generate a patch set to send to the server with json-patch [1] semantics.
I highly recommend the excellent fast-json-patch library [2] that makes this process a breeze.
Actually, I use its deepClone method outside of json-patch scenarios, it's so well implemented.
[1] https://jsonpatch.com/ https://jsonpatch.com/
[2] https://www.npmjs.com/package/fast-json-patch https://www.npmjs.com/package/fast-json-patch