Refactoring code with return statements

The "if" blocks with checkcustomers are exactly used in other methods of this class, so there are many duplicates of the code for the same checks. But I cannot also directly extract these checksums for a single method, because they have return values.

Some good ideas for refactoring this code? I just modified this code to simplify here, so don't catch the minor issues in this code (if any). Basically the question is how to extract a piece of code into a method (since it is duplicated in other ways) when there are many returns in this current method.

public Details getCustomerDetails(){ if(checkifcustomerhasnoboobs){ ..worry about it.. return new Details("no"); } if(checkifcustomerplaytenniswell){ ..do find a tennis teacher return new Details("no cantplay"); } //...ok now if customer passed the test, now do the some real stuff // // CustomerDetails details= getCustomerDetailsFromSomewhere(); return details; } 
+4
source share
5 answers

How about this?

 public Result checkSomethings() { if ( checksomething1 ) { return ResultCheckSomething1; } if ( checksomething2 ) { return ResultCheckSomething2; } return ResultCheckNone; } public Details getCustomerDetails(){ Result result = checkSomethings(); switch ( result ) { case ResultCheckSomething1: return new Details("message1"); case ResultCheckSomething2: return new Details("message2"); default: return getCustomerDetailsFromSomewhere(); } } 

Result... codes Result... will be in the listing.

+2
source

Maybe something like this?

  public Details getCustomerDetails(){ boolean isError = checksomething1() || checksomething2(); String message = checksomething1() ? "message1" : "message2"; return isError ? new Details(message) : getCustomerDetailsFromSomewhere(); } 

If you try to avoid call verification features twice, just keep them in the results

 public Details getCustomerDetails(){ boolean check1 = checksomething1(); boolean check2 = checksomething2(); String message = check1 ? "message1" : "message2"; return (check1 || check2) ? new Details(message) : getCustomerDetailsFromSomewhere(); 

}

+1
source

Replace assignment returns of a result variable that remains zero until the first assignment to it. Each block can be replaced by a function that returns null if its condition for changing the result is false.

As pointed out in herman's comment, this only works if null is not a possible result of one of the calls.

 public Details getCustomerDetails(){ Details result = null; if(checksomething1){ ..error result = new Details("message1"); } if(result == null) { if(checksomething2){ ..error result = new Details("message2"); } if(result == null){ result = getCustomerDetailsFromSomewhere(); } return result; } 
+1
source

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.

0
source

Using Java 8, you can reorganize a method that returns Optional<...> . Expressions like return x; will be replaced by return Optional.of(x) (if x cannot be null). The default return Optional.empty() at the end will be return Optional.empty() .

Then you can use return optional.orElseGet(() -> ...)) to calculate the value for the case when none of the original return statements is reached.

0
source

All Articles