Java – when a statement is considered a single entry / single exit, when it is not?
•
Java
I'm not sure if I can use it well. For example, if statements in Java are called single entry / single exit statements
if(someCondition) doSomething();
What are examples of non (single / single exit) declarations?
Solution
One exit point method (single exit):
public int stringLength(String s) {
return s.length();
}
Two exit point methods:
public int stringLength(String s) {
if(s == null) {
return 0;
}
return s.length();
}
The following is a quote from Martin Fowler's book Refactoring:
And illustrate the above statement with examples to compare the codes of the two methods:
double getPayAmount() {
double result;
if (_isDead) result = deadAmount();
else {
if (_isSeparated) result = separatedAmount();
else {
if (_isRetired) result = retiredAmount();
else result = normalPayAmount();
};
}
return result;
};
And there are some exit points:
double getPayAmount() {
if (_isDead) return deadAmount();
if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();
return normalPayAmount();
};
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
