Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:38:16+00:00 2026-06-13T10:38:16+00:00

if you have any suggestions on how to modify the question title in order

  • 0

if you have any suggestions on how to modify the question title in order to be more descriptive, please feel free to say so .

Suppose you have a class method that returns an Object, is there a best practice or standard way of where to create the object within the method ? To clarify, see below :

public MyCustomObject myMethod(String arg1, String arg2){
    try{
        if (something){
            ...
        } else {
            ...
        }
    } catch ( SomeException e ){
        ...
    } catch ( SomeOtherException e ){
        ...
    }

    return myCustomObject;
}

MyCustomObject has an empty constructor, a constructor with 5 fields and getters/setters. I need to return a valid MyCustomObject in every case of the (simplified) flow above. Please do not focus on the control flow itself.

I think that I can either :
a) Initialize a variable of type MyCustomObject with null in the beginning of the method. Assign a new MyCustomObject to it for every different case in the control flow. Return it in the end.
b) Instantiate a MyCustomObject in the beginning by using the empty constructor. Modify the object with the setters for each of the cases in the flow.

Can you think of reasons why one of the above or a different way is preferable ?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:38:17+00:00Added an answer on June 13, 2026 at 10:38 am

    I prefer option 3 which you didn’t mention: Create a new MyCustomObject for every different case in the control flow, and return it immediately. No variable required, usually:

    if (foo) {
        return new MyCustomObject("foo");
    }
    if (bar) {
        return new MyCustomObject("x", "y");
    }
    // etc
    

    This usually ends up with less nesting, and it’s easier to read, in that as soon as you hit the return statement, you’re done.

    Some people hold on to the dogma of only having a single exit point, but I regard that as somewhat pointless these days – it made sense in languages where you needed to do clean-up before returning, but with garbage collection and finally blocks for other resource clean-up, it’s unnecessary.

    If you return as soon as you can, you can often end up with less nesting within the code – which greatly increases readability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Anyone have any suggestions on how to make randomized colors that are all greenish?
Does anyone have any suggestions on how to edit an <a href=''> link in
Does anyone have any suggestions about where I can find a C# implementation for
do you have any good suggestions how to backup remote php site and mysql,
I'm looking for suggestions as well as any benchmarks or observations people have. We
Anyone have any ideas what I can do? Please reserve commenting about using 1.1
//Edit: Really, nobody has any suggestions or thoughts on this? Have I asked the
Does anyone have any practical suggestions about how to manage feature creep in GUIs?
i have againg a problem with completition. now i can't get any suggestion. Sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.