我相信这部分与短路逻辑有关,但我找不到任何直接回答我的问题的问题。可能的相关问题:Benefits of using short-circuit evaluation,Why use short-circuit code?快速退出方法或构造函数是否有好处?
考虑以下两个代码块,这两者都是一类
public MyClass(OtherClass other){
if (other != null) {
//do something with other, possibly default values in this object
}
}
可能的构造,这
public MyClass(OtherClass other){
if (other == null) return;
//do something with other, possibly default values in this object
}
有没有做任何好处后者比前者?在构造函数中没有其他代码,只是使用other
对象构造此代码的代码。
这也是我的想法。只是想看看是否有一个表现(或任何其他)的好处,以一种方式做对比。 – 2012-03-28 16:55:15