- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我不确定我是否理解得很好,例如Java中的if语句被称为单入口/单导出语句。如果它的条件为真,这是否被认为是它的单一入口点,如果条件为假,它是否被认为是它的单一导出点?
if(someCondition)
doSomething();
非-(单次进入/单次退出)语句的示例是什么?
最佳答案
一个导出点法(single-exit):
public int stringLength(String s) {
return s.length();
}
两种退出点方法:
public int stringLength(String s) {
if(s == null) {
return 0;
}
return s.length();
}
下面引用自 Martin Fowler 的书Refactoring:
I often find I use Replace Nested Conditional with Guard Clauses when I'm working with a programmer who has been taught to have only one entry point and one exit point from a method. One entry point is enforced by modern languages, and one exit point is really not a useful rule. Clarity is the key principle: if the method is clearer with one exit point, use one exit point; otherwise don't.
和上面语句的一个例子,比较这两种方法的代码做同样的事情:
double getPayAmount() {
double result;
if (_isDead) result = deadAmount();
else {
if (_isSeparated) result = separatedAmount();
else {
if (_isRetired) result = retiredAmount();
else result = normalPayAmount();
};
}
return result;
};
还有一些导出点:
double getPayAmount() {
if (_isDead) return deadAmount();
if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();
return normalPayAmount();
};
Nested conditional code often is written by programmers who are taught to have one exit point from a method. I've found that is a too simplistic rule. When I have no further interest in a method, I signal my lack of interest by getting out. Directing the reader to look at an empty else block only gets in the way of comprehension.
关于java - 什么时候说语句是单次进入/单次退出,什么时候不是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17295315/
当用户在 uisearchbar 中键入文本时,我正在过滤一个数组,但问题是我有一个警报处理程序,每次调用委托(delegate)时都会触发该处理程序,但我希望警报出现只有一次没有多次......代码
我有一个 HTML5、jQuery 卡片内存游戏,您可以通过一次翻转两张卡片来匹配卡片。我想在两张卡片匹配时播放动画,但因为我已经将 "transform: rotationY(180deg)" 应用
在我的 Jboss-EAP-6.1 中,我部署了一个名为 'myRealWebApp.war' 的 .war我可以使用此网址访问我的应用程序 - http://mywebsite.com/myReal
我是一名优秀的程序员,十分优秀!