Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP: Passing an object vs. passing attributes into a method

In terms of a clean design, which is preferable?

1) Passing an object, say yourself, to another class method which will manipulate the attributes of the passed object directly.

Inside Class A...

B.doStuff(this);

2) Pass the object's attributes instead, and either assign the return back to the attributes, or pass by reference.

Inside Class A...

this.var1 = B.doStuff(this.var1);

The downside to the first method is ambiguity, it isn't explicit what B is altering in A. The downside to the second method is that unless attributes are passed by reference/pointers, then an array would need to be returned, and would also constitute a longer function call if you end up passing a lot of attributes. I'm guessing the correct choice is dependant on the situation, but are there other advantages/disadvantages anyone else can come up with before I make a decision?

EDIT: Due to the fact that there will be a large amount of attributes passed to the second class (I am delegating certain tasks to it), I think it warrants the use of the first method in this case. Another downside I came up with was that Class B can only access public attributes and methods in Class A, whereas the second method allowed Class B to manipulate private attributes that Class A may not wish to reveal to the public. But once again, using the second method is more common and certainly more preferable in cases where only a few variables will be accessed.

like image 816
Aram Kocharyan Avatar asked Oct 12 '22 16:10

Aram Kocharyan


1 Answers

You should opt for that design which provides least amount of information to the function but optimum enough for the working of the function. This ensures least amount of side effects to expect from the processing.

I think this question comes down to whether to pass value by reference or by value.

Mostly the functions are supposed to least to no side effects but if your Intention of function is to process the Data being passed in the arguments then passing the whole class reference is more better.

For Example when you take a reference to a Graphic objects in a Forms paint event (in C#), it is known that the manipulation will be done on the Object reference passed(But surely it depends upon your requirement and Implementation).

If your function can work properly without requirement of whole Class reference being passed then you should again opt for that. But remember passing lots of parameters by value are more resource intensive as a new copy is to be created every time and so new Memory is to be allocated, where as in by reference you are just passing a reference so no New memory is being allocated.

You second design can be used if you want your method to have no side effect on the data passed instead it works on the data provided and creates a new Object that contains the result, which you in turn assign to required attributes.

Usually the perception of the readers of your code is that, if there is some function being called on the Right side of the assignment operator then he know that something is done with data but a new Data is being returned and hence he generally doesn't expect it to do manipulation to the data provided as Parameter. If reader only see a function call without any assignment operator then he expect that something is done to the data being provided as Parameter.

like image 122
Shekhar_Pro Avatar answered Oct 14 '22 07:10

Shekhar_Pro