I believe this to be related partially to short-circuiting logic, but I couldn't find any questions that directly answered my question. Possible related questions: Benefits of using short-circuit evaluation, Why use short-circuit code?
Consider the following two code blocks, both of which are possible constructors for a class
public MyClass(OtherClass other){
if (other != null) {
//do something with other, possibly default values in this object
}
}
and this
public MyClass(OtherClass other){
if (other == null) return;
//do something with other, possibly default values in this object
}
Is there any benefit to doing the latter over the former? There is no other code that follows in the constructor, just code that uses the other
object to construct this one.