Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing whole object vs passing object's property

I'm thinking about the solution for my application. Here's the situation: I have a class with a method that takes ObjectA as an input parameter and calls several small methods. Each one of these methods needs some parts of the ObjectA (they don't overlap, i.e. method1() needs ObjectA.field1 and ObjectA.field2, method2() needs ObjectA.field3 and so on...)

Given the general good code practices and performance, is it better to pass ObjectA to each one of these methods so they can extract the value they need on their own or is it better just pass them values? I mean:

method1(ObjectA); method2(ObjectA); 

or

method1(Object1.getField1(), ObjectA.getField2()); method2(ObjectA.getField3()); 
like image 846
Lucas Avatar asked Jan 15 '14 11:01

Lucas


People also ask

What are the different way to pass object as argument?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

What happens when you pass an object to a method?

Note: When an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that its corresponding argument does.

What is passing an object as a parameter?

Passing an object as a parameter is a lot like passing any other parameter. You just need to use the name of the object class, in your case Fruit , in place of any other variable type. It's also a lot like how you would define the instance variables of type Fruit in your FruitBasket class.

When you pass a object by reference what exactly is passed?

two kinds of values that can be stored in variables, passed as arguments, returned by methods, and operated upon: primitive values and reference values." Object references are values. Everything is passed by value. The operative implication: f(x) (passing a variable) will never assign to x itself.


2 Answers

Keep in mind, with your code, you're not actually passing ObjectA. Namely, you're passing the reference type to ObjectA, so on a performance note the difference between passing a String object reference and a ObjectA object reference would be negligible.

The way I would write it

I would pass the whole object, if the method is pertinent to the class. My reasoning for this is to split up class knowledge as much as possible. What I mean by this is the following.

public void doSomethingRelatedToTheClass(String param) {     // Do something with param. } 

My first criticism here is that this method assumes that the input is the correct field. My second, is that now, the class calling this code needs to know a little bit more about this method, because it has to call it like this:

doSomethingRelatedToTheClass(myObject.getValue()); 

And what this means is, if you find that another member of ObjectA works better inside this method, or you want to access other members of ObjectA, and you change doSomething() to reflect this change, you also need to change the method call, to:

doSomethingRelatedToTheClass(myObject.getOtherValue(), myObject.getValue()); 

So by passing in the whole object, you abstract that detail away, and the method can handle it; namely:

doSomethingRelatedToTheClass(myObject); // Doesn't need to know what you do with it.  public void doSomethingRelatedToTheClass(ObjectA object) {     String val = object.getValue();      String otherVal = object.getOtherValue(); } 

When a change to one class, results in a change in other classes, this is an Anti-pattern called Shotgun Surgery.

Edit

I've had chance to review my answer here and I've amended my original answer slightly because I believe it isn't the best solution for all situations. As above, if a method is related to a class specifically, then the instantiation of that class (or more preferably, its superclass or implemented interface[s]) should be the parameter.

The time this is not the case is when the functionality can be generic. An example of a generic function might be:

public String[] findNouns(String sentence); 

In this case, finding the nouns in a sentence might be appropriate for lots of use cases, and not just the use cases that you have defined. As such, passing in the value is the only sensible approach because otherwise, you couple two pieces of logic together that have no direct relationship. The finding of nouns and the arbitrary object you have defined.

In Summary

  • If the method is logic that is related to the object, pass in the object

  • If the method has nothing to do with the object, and the object is just using it as a utility function, then pass in the value and name the function generically.

like image 198
christopher Avatar answered Sep 21 '22 21:09

christopher


Let's examine a scenario. Now this may or may not be your scenario but it illustrates a point.

Lets say field1 and field2 in your case are two integers and method1 sums them and returns the result.

If you pass in the objects then that method can only ever sum those two fields. The method is also now strongly coupled with those objects.

On the other hand, if you pass in only the fields, the two integers in this case your method becomes more generic. You can now sum any 2 arbitrary integers regardless of which objects they are on.

In general though, always expose as little of your objects to other methods and classes. This promotes loose coupling.

Exceptions

AS maaartinus points out if for example field1 and field2 were Points and method1 calculated the distance between those two points, then I would have to agree that passing two Points would be better than passing 2 xy integer pairs (4 parameters)

Hope this helps

like image 33
n4rzul Avatar answered Sep 22 '22 21:09

n4rzul