Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing copy of object to method -- who does the copying?

Tags:

oop

I have an object that I'm passing in a method call. Say I'm using a language that only allows you to pass objects by reference, like Java or PHP. If the method makes changes to the object, it will affect the caller. I don't want this to happen. So it seems like I need to make a copy of the object.

My question is: whose responsibility is it to clone the object? The caller, before it calls the method? Or the callee, before it changes the object?

EDIT: Just to clarify, I want this to be part of the contract of this method -- that it never modifies the original object. So it seems like it should be up to the method to make the copy. But then the caller has no protection from a method that doesn't do this properly. I guess that's acceptable -- the only other alternative seems to be to have this built into the language.

like image 862
JW. Avatar asked Oct 08 '08 15:10

JW.


People also ask

Does object assign do a deep copy?

assign() is used to copy the values and properties from one or more source objects to a target object. It returns the target object, which has properties and values copied from the source object. Since Object. assign() copies property values, it is unsuitable for deep cloning.

How are objects copied in Python?

Copy an Object in Python In Python, we use = operator to create a copy of an object. You may think that this creates a new object; it doesn't. It only creates a new variable that shares the reference of the original object.

Which method is used to copy from an existing object?

Creating a copy using the clone() method The class whose object's copy is to be made must have a public clone method in it or in one of its parent class. Every class that implements clone() should call super. clone() to obtain the cloned object reference. The class must also implement java.

Which is used to copy the objects?

There are several ways to copy an object, most commonly by a copy constructor or cloning. Copying is done mostly so the copy can be modified or moved, or the current value preserved. If either of these is unneeded, a reference to the original data is sufficient and more efficient, as no copying occurs.


4 Answers

Generally, the caller should make the copy if it is concerned about changes. If the caller doesn't care, the method should make the copy if it needs to do something that it knows shouldn't persist.

like image 117
Joe Skora Avatar answered Dec 14 '22 23:12

Joe Skora


So you want to do something like

MyObject m = new MyObject(); MyObject n = MyObjectProcessor.process(m);?

It seems simpler to me to do something like MyObject n = MyObjectProcessor.alter(m.clone());

where it's clear who's doing what to who. You could make the argument that the processor class function should be free of side effects, i.e. it should return a new object any time it's going to change state, but (AFAIK) that's not so consistently followed in OO as opposed to functional programming.

Something like the above is probably harmless, as long as it's clearly named and documented.

like image 20
Steve B. Avatar answered Dec 14 '22 23:12

Steve B.


We could look at ruby for guidance. They use a ! symbol to indicate that an object is modified in-place. So, salary.add(10000) returns a new object but salary.add!(10000) returns the original object but modified. You could use the same idea in Java or PHP by using a local naming convention.

like image 30
David Medinets Avatar answered Dec 15 '22 00:12

David Medinets


The caller. Because, sometimes you want to make changes to the objects themselves and other times to a copy.

Although, I consider it a bad practice for callee to modify passed objects (at least in object oriented languages). This can cause many unwanted side effects.

(after your) EDIT: In that case it is callee's responsibility to enforce the contract, so there are two options:

  • The callee simply does not modify the object
  • or the callee copies the object and works with the copy afterwards
like image 45
Marko Dumic Avatar answered Dec 15 '22 01:12

Marko Dumic