mardi 15 novembre 2022

How to solve flow finish problem in if else statement

I am facing a problem where there are too many if conditions and also multiple return statements. When I write the code like this, it becomes a very difficult code to read. My goal is to get rid of if else statements as much as possible and to finish the code in one place.

Below I will try to explain the problem I am having with a code example.

Let's consider a code where too many external services are called and a response check must be made for each service. For Example:

public class ResponseExternal{
   private Header header;
   private Body body;
   //get set methods
 }       
public class ResponseExternalAnother{
   private Header header;
   private Body body;
   //get set methods
 }   
public class Response{
   private Header header;
   private Body body;
   //get set methods
 }       

Execute method in service class:

   public Response run(Request request){
    
       
       //Some implementation for external services request
    
       ResponseExternal responseExternal = ResponseExternalService.callExternalService(externalServiceRequest);
       if(!responseExternal.getHeader().isSuccess()){  //I have to control external service response but I don't want this statement to be here. Coding these controls reduces the readability of the code.
          response.setResponseHeader(responseExternal.getHeader());
          return response; //I have to finish flow here
       }
    
       if(Objects.nonNull(responseExternal.getBody() && Objects.nonNull(responseExternal.getBody().getExampleField())){
         ExampleField exampleField = responseExternal.getBody().getExampleField();
       }
    
    
       //continue process
     
      ResponseExternalAnother responseExternalAnother = ResponseExternalService.callExternalService2(externalServiceRequest2);
       if(!responseExternalAnother.getHeader().isSuccess()){ //I have to control external service response but I don't want this statement to be here. Coding these controls reduces the readability of the code.
          response.setResponseHeader(responseExternalAnother.getHeader());
          return response; //I have to finish flow here
       }
    
    
      if(Objects.nonNull(responseExternalAnother.getBody() && Objects.nonNull(responseExternalAnother.getBody().getExampleField())){
         ExampleField2 exampleField2 = responseExternalAnother.getBody().getExampleField();
       }
    
    
       
       //I have many conditions like this

   }

I inspected this topic: Replacing if else statement with pattern. This solution couldn't resolve my problem. This solution solve if else statements but I think it didn't solve finish flow problems.

I need to something like that:

public Response run(Request request){

ResponseExternal responseExternal = ResponseExternalService.callExternalService(externalServiceRequest);

// In case of a failed response from the external service, the flow should be interrupted here and the Response object returned working like this "return response;"

ResponseExternalAnother responseExternalAnother = responseExternalService.callExternalService2(externalServiceRequest2);

// In case of a failed response from the external service, the flow should be interrupted here and the Response object returned working like this "return response;"


 // I want to only return one response.

}

I am looking for a solution method that solves both problems.

Aucun commentaire:

Enregistrer un commentaire