user10419944
user10419944

Reputation:

Is it preferable to pass the whole object or call a method on it and pass the returned value?

I am providing the examples below to further illustrate my point:

Example 1:

public class A {

    public String getTheNeededString() {
        String returnedString;        
        //logic goes here
        return returnedString;    
    }
}

public class B {
    public void doSomething(A objectA) {
        String neededString = objectA.getTheNeededString();
        //proceed to do something that needs the above String
    }
}

public class Client { 
    public static void main(String[] args) {
        A objectA = new A();
        B objectB = new B();
        objectB.doSomething(objectA);
    }
}

Example2:

public class A {
    //stays the same
}

public class B {
   public void doSomething(String neededString) {
       //proceed to do something that needs the above String
   }
}

public class Client { 
    public static void main(String[] args) {
        A objectA = new A();
        String neededString = objectA.getTheNeededString();
        B objectB = new B();
        objectB.doSomething(neededString);
    }
}

I guess that there might not be a "better" approach and it might be a matter of preference. If that's the case, then I would really appreciate any opinion/preference on the matter.

Upvotes: 0

Views: 71

Answers (3)

mentallurg
mentallurg

Reputation: 5207

It depends on what is the goal of each class in your design.

Just an example.

Suppose class A represents a product and provides some details based on which class B creates product description. Suppose this logic is more complex that just getting a string. If you encapsulate such logic into the class B (approach 1), then if the logic changes later on, the only place to adjust will be the class B. The effort will be small and the number of bugs also small, if any. In case of the 2nd approach you will have to find all places where you pass data of object of class A to the object of class B and adjust all of them. It will take more time. Also it can happen, that you do these changes slightly differently and introduce more bugs. In such cases 1st approach is preferable.

Suppose the goal of the class B is to translate a given message key to some language, e.g. according to the default locale. In such case this class does not need to know where this key comes from (to reduce dependency on other classes). Suppose you have also other classes that provide keys that need to be translated, but they are in different class hierarchies and such objects cannot be passed to the method of class B. Then the 2nd approach is better. Each place where B is called implements its specific logic and passes to B only the common part, String key.

Upvotes: 0

shailesh88
shailesh88

Reputation: 288

It is always preferable to pass only the required info for following reasons:

  1. Single-responsibility principle : Each class/method should have one and only one responsibility. In this case, the method doSomething() should only know how to transform a string input into the desired output. It should not be the responsibility of this method to get the string by calling other methods.

  2. Loosely coupled design: Imagine you decide to change the getTheNeededString() method in a way that you have to change all the calls to this method in your project. You'll have to make changes in doSomething() method as well. Here, the doSomething() method is tightly coupled to getTheNeededString() method. This is not a good design.

Upvotes: 2

Brice Frisco
Brice Frisco

Reputation: 450

I believe it really is a matter of preference, and dependent on what exactly you need the program to do.

I would advise with Example 2, as it just makes more sense to me. If I were to come across your code at a later date and trying to interpret it, the second approach would be much more intuitive, at least for me.

It also makes the code reusable in the case you need to perform that same operation with any String outside of Object A. That may not be the case, but I still do not see the need to pass in the entire object. Pass the bare minimum input needed to produce the output needed.

Assume the method is to, say, count the number of occurences of the letter 'Z' and is called with countOccurencesOfLetterZ(objectA). Now, any person just exposed to this code will have to inspect this method to find out what it does - sure, the name is very intuitive.. but you're passing in the entire object, which needlessly creates uncertainty/confusion as to what you're trying to accomplish.

Now, assume you have countOccurencesOfLetterZ(objectA.getUsername()). I no longer have to inspect that method to figure out what you're trying to accomplish. Even better, if this method ends up being something you can utilize outside of Object B, then you can put it in a utility class and reuse that code for any string.

Upvotes: 0

Related Questions