Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we should not call public method from another public?

Tags:

java

I was told by someone that we should not call a public method of a class from another public method in same class. Now i am not able to understand this as i dont see any problem with that. Once a method has been declared public then its contract is fixed for lifetime and hence there should not be any problem in calling it from another public method.

So I am not sure if that statement is true or its ok to call public api from another public api [This is from design perspective]?

like image 375
Lokesh Avatar asked Feb 07 '13 10:02

Lokesh


6 Answers

In my opinion you should avoid call a public method within another method because of inheritance. Consider classes:

public class Parent {

    //return sum
    public double getSum(double... value){
        //implementation
    }

    //return average
    public double getAverage(int count){
        //call getSum
        double sum = getSum(20, 40, 60);
        return sum / count;

    }
}

public class Child extends Parent {

    @Override
    public int getSum(double... obj){
        // change implementation
        // always return 100;
    }
}

If you called on Child object getAverage method, you would get some unexpected value, whole interface of Child object is broken. You have to override getAverage method too...

Example with String class

1462    public boolean startsWith(String prefix) {
1463        return startsWith(prefix, 0);
1464    }

is from this perspective wrong, because string is final, so you can't inherit it and override its public methods..

like image 94
nick-slotr Avatar answered Oct 22 '22 14:10

nick-slotr


When you very often call public methods from other public methods of the same class, it probably means you have unnecessary utilities methods. And maybe you should try to be a little more DRY so as to ease maintenance and keep your API easy to grasp.

But that's just a warning, it's perfectly valid to call a public method from another one, and you'll find many examples of java.lang standard code doing just that.

An example from java.lang.String :

1462    public boolean startsWith(String prefix) {
1463        return startsWith(prefix, 0);
1464    }

There would be no point in making some methods private just to enforce a rule about public methods not calling other public methods.

like image 35
Denys Séguret Avatar answered Oct 22 '22 15:10

Denys Séguret


I just summarize answers here and add some context:

  • It is legal compile-wise to reuse public methods from public methods within the same class.
  • It is considered a bad practice for API design,because it makes your class designed error-prone for inheritance given one may override one method without overriding another, breaking the logic of your class (this point is from one famous Java "best-practices" book "Effective Java" and it is well explained there and it is also well explained in another answer in this question)
  • Also it is mentioned in another answer, you can not mock public method, so it may be concern for unit-testing

My 2 cents:

  • Making a class "final" makes it not a bad practice anymore, as you do not design your API for inheritance.
  • I don't see a concern with unit-testing, you can write your test the same way as you would write it if one of your methods is private.
like image 23
Dmitrii Stebliuk Avatar answered Oct 22 '22 14:10

Dmitrii Stebliuk


Does your compiler balk at you when you try? No? Then it's legal in that respect.

Does the person providing this 'advice' produce any canonical document explaining the standard (either in the industry or within your organisation)? No? Then it's opinion.

Consult your company standards, but otherwise, I call nonsense.

like image 11
Grant Thomas Avatar answered Oct 22 '22 16:10

Grant Thomas


If you call a public methods from other public methods it makes unit testing more complicated. If one method is depended on another method of the same class you can't mock it to test it separately. So you may have to write test code for the same method twice.

see also Unit testing a method that calls another method

like image 11
Max Schmidt Avatar answered Oct 22 '22 16:10

Max Schmidt


I am not sure what he meant. You can obviously call a public method from another public method. It is not a bad idea - even Java's source has methods in which one public method calls another public method.

Why don't you ask the guy who advised you to explain the reason? I think he is in a better position to tell than we are.

like image 1
Aniket Inge Avatar answered Oct 22 '22 14:10

Aniket Inge