I would do this:
public Details getCustomerDetails(){ Details invalidDetails = checkForInvalidCustomer(); if (invalidDetails !=null) { return (invalidDetails); } //...ok now if customer passed the test, now do the some real stuff // // CustomerDetails details= getCustomerDetailsFromSomewhere(); return details; } public Details checkForInvalidCustomer() { if(checkifcustomerhasnoboobs){ ..worry about it.. return new Details("no"); } if(checkifcustomerplaytenniswell){ ..do find a tennis teacher return new Details("no cantplay"); } // nulls means valid customer return (null); }
Basically, for your specific example, I use null so that I can distinguish between the case where none of the conditions met, none of the conditions were agreed. That way I can use one if statement. Now, if you want to return null, you need to change this solution a bit, perhaps use some constant in order to mark the case, and not use null.
source share