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.
This a case where you should do what is the most readable to you and your coworkers. There is unlikely any perceivable speed difference between the two approaches.
The only difference is readability. With what you call "quick exit", you can stop thinking about what conditional you're inside of, because you're not inside of one anymore.
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