I have one method which need to return multiple values whether it was successful or not, what the key used to call the api and if successful the return result. Here is more concrete method,

public xxxResult PublishMessage(){

   // dynamically calculate the key
   try {
         // call the third party api and get the result
         // return key-used, result and successful flag
   }
   catch(Exception ex){
      // return key-used, null result and not-successful flag
   }
}

Is it make sense to create a xxxResult class here or is there any other way design that I missing here? When should we use xxxResult pattern.

up vote 5 down vote accepted

I hate xxxResult / xxxReponse classes. Its just bad naming

They normally seem to appear when you are serialising a response to a json object to pass back from a REST API and want to include some meta data, such as an error occuring as well as the actual result.

But there are better ways of handling this senario, namely returning a HTTP error code and an error message instead of a result.

In your particular case I would seperate the key generation and message publishing concerns to avoid the compound return type and to avoid the problem of an actual crash rather than an exception meaning you lose a key

Other cases I've seen are where you do have a legitimate 'Result of a Proccess' complex Type which really has no meaning on its own. Here you just need to get more inventive with your naming if you want to avoid many xxxResult classes.

The ReportGenerationResult is a Report, the ToolCalibrationResult is an ErrorMeasurement etc

  • how to tell the method invoker whether api was successful or not? If successful then the result – user960567 Nov 11 at 9:33
  • if successful HTTP code 200 + json. if not 500 and error message in body – Ewan Nov 11 at 10:00
  • You mean I should return HttpResponse? – user960567 Nov 11 at 10:18
  • if you are using HTTP then you have no choice. If its just local methods you can just throw an exception – Ewan Nov 11 at 10:58
  • Its Aws SnsClient – user960567 Nov 11 at 12:18

If your language supports tuples, this may be a solution. Tuples have several benefits in this case:

  • The method is easier to write.

    try {
        return (true, obj);
    } catch (...) {
        return (false, null);
    }
    
  • The caller code is simpler as well.

    var success, response = PublishMessage();
    
  • There is no need to create an additional class.

If the language doesn't support tuples, your other option is to have the method return one type on success, another type on failure. The caller will have to check the type of the returned value, and act accordingly for one type or another.

try {
    return new Key(...);
} catch (...) {
    return new ApiError(...);
}

Yet another solution is to have a magical value: say null to indicate an error. While simpler than the previous approach, I wouldn't recommend it. Having two types makes it possible to extend the response later. For instance, the response returned on error can contain the error code or the error message.

  • 2
    If failure means no response, and a response means success, there's no need for a tuple. – Deduplicator Nov 10 at 21:32

Consider a little polymorphism. This

public xxxResult PublishMessage(){

   // dynamically calculate the key
   try {
         // call the third party api and get the result
         // return key-used, result and successful flag
   }
   catch(Exception ex){
      // return key-used, null result and not-successful flag
   }
}

can become

public Message PublishMessage(){
   Message message; 

   // dynamically calculate the key
   Key key = calculateKey();

   try {
         // call the third party api and get the result
         APIResult apiResult = thirdPartyAPI.getResult();

         // return key-used, result and successful flag
         message = new MessageSuccess(key, apiResult);
   }
   catch(Exception ex){
      // return key-used, null result and not-successful flag
      message = new MessageFailure(key);
   }
   finally{
       return message;
   } 
}

The caller will NOT have to check the type of the returned value. Whatever code is using this doesn't have to know or care what happened. The exception has been handled and the message itself will know what to do and has everything it needs to do it. One way or the other.

But please use better names than these.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.