1

In a large code base, what is a good practice between: Updating class properties inside the method vs. returning a value from the method and updating the property in the place where the call to the method was.

Example 1: Updating class properties inside the method

def sendFileToS3OrFileServer(Request request) {
 if (settings.isS3())
 request.s3Url = helper.uploadToS3(request.absoluteFilePath)
 else if (settings.isFileServer())
 request.fileServerUrl = helper.uploadToFileServer(request.absoluteFilePath)
}
//call the method above
sendFileToS3OrFileServer(request)

Example 2: Returning from the method instead of updating inside the method

String sendFileToS3OrFileServer(Request request) {
 if (settings.isS3())
 return helper.uploadToS3(request.absoluteFilePath)
 else if (settings.isFileServer())
 return helper.uploadToFileServer(request.absoluteFilePath)
}
//call the method above
request.commonS3OrFileServerUrl = sendFileToS3OrFileServer(request)
asked Jul 26, 2016 at 16:27
2
  • I would like to help but your question is a bit unclear, because I can't tell what the code is trying to accomplish; my advice is to either simplify your code into the essence of the problem (toy example), or give us enough context (class definitions, etc) so we can understand the full example. Commented Jul 27, 2016 at 1:43
  • Yes more context please - specifically why is Request mutable, and the need for it to hold on to an S3 or a FileServerUrl? Are you trying to capture a Response to handle the action of uploading to S3 or a FileServer instead? As a side note, the uploadXXX() methods returning a url seems a bit awkward, as I would have expected the upload method to do one thing and one thing only - i.e. perform the upload function and maybe return a boolean indicating a success or a failure, not a url of where it uploaded to. It is unclear as to what you want to do with the url. Commented Jul 27, 2016 at 4:43

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.