0

I've seen a lot of questions about what is shallow vs deep copy, but not a lot on when to use each. If deep copy is safer, why aren't more developers deep copying every single object every time something is changed? If that were done, doesn't performance become an issue? Is there ever a good time to shallow copy?

Edit - This question is not a duplicate of the one provided. That question asks what is the difference between the two. This question is asking in what scenario is it appropriate to use each one, I understand the differences.

Edit - The question is if a server is receiving requests and adding or modifying one field, should the server map the json request to object, then map every single field from that object to new objects with the one field modified then forward the request to a second api? Or is a shallow copy sufficient since there's not really a chance of the original object being modified?

stratg5
  • 27
  • 1
  • 6
  • This isn't an answer but I encourage you to read this an contemplate it a bit before you do more searching on your question: https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy – JockM Mar 22 '18 at 19:02
  • @JockM Please see my edit. I understand the difference between the concepts, my question is in which scenarios is it appropriate to use each type. – stratg5 Mar 22 '18 at 19:04
  • If you need a deep copy, then you have to create a deep copy. Otherwise you can create a shallow copy. You don't really understand the difference if this is unclear to you. Read the dupe, it has great answers. – Kayaman Mar 22 '18 at 19:05
  • @Kayaman In which scenarios would I need a deep copy? I do understand the difference. I want to hear other opinions because there is some disagreement between myself and other developers – stratg5 Mar 22 '18 at 19:06
  • 1
    When you don't want to share the inner objects, but create separate ones. Imagine if you had a shallow copy of a house, and all the furniture is shared. If you have disagreements with developers, edit your question to include relevant information, i.e. your and the other developers' opinions. – Kayaman Mar 22 '18 at 19:10
  • @Kayaman Added second edit with specific scenario – stratg5 Mar 22 '18 at 19:19
  • @stratg5 the problem is your question is too broad,I have been exactly where you have been when presented with this very idea (in Actor for Windows 2.0 of all things) but the answer really is in that link. A shallow copy is for when you don't need complete separation of all data, and a deep copy is for when you do – JockM Mar 22 '18 at 19:20
  • 1
    Creating defensive copies (which is what this question seems to really be about) of anything requires you to write more code, execute more code and in general make more of an effort. In return you get the security that objects can't be modified by "hostile" parties. It's up to you to decide whether it's worth the effort or not. – Kayaman Mar 22 '18 at 19:24
  • 1
    Duplicate or not, it's still asking for opinions and that alone is justification for closure. This is not a programming issue. Question does not meet the standards of https://stackoverflow.com/help/mcve – June7 Mar 22 '18 at 19:45
  • Ok then I expect every question that asks when to use x over y to be closed because they are all opinion questions. – stratg5 Mar 26 '18 at 18:40

0 Answers0