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]?
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..
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.
I just summarize answers here and add some context:
My 2 cents:
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With