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 597211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:15:21+00:00 2026-05-13T16:15:21+00:00

Given that multiple return statements are acceptable (I sort of disagree, but let us

  • 0

Given that multiple return statements are acceptable (I sort of disagree, but let us digress), I’m looking for a more acceptable way to achieve the following behavior:

Option A: multiple returns, repeated code block

public bool myMethod() {
    /* ... code ... */

    if(thisCondition) {
        /* ... code that must run at end of method ... */
        return false;
    }

    /* ... more code ... */

    if(thatCondition) {
        /* ... the SAME code that must run at end of method ... */
        return false;
    }

    /* ... even more code ... */

    /* ... the SAME CODE AGAIN that must run at end of method ... */
    return lastCondition;
}

It makes me feel dirty to see the same (little) code block repeated three times each time the method returns. Furthermore, I would like to clarify that the two return false statements above can certainly be described as returning mid-method… they are absolutely not “guard statements.”

Is Option B slightly more acceptable? I feel that I may abusing try/finally, and I’m hoping there is something completely different that I should be doing.

Option B: multiple returns, try/finally block (without catch blocks / exceptions)

public bool myMethod() {
    try {
        /* ... code ... */

        if(thisCondition) {
            return false;
        }

        /* ... more code ... */

        if(thatCondition) {
            return false;
        }

        /* ... even more code ... */

        return lastCondition;
    } finally {
        /* ... code that must run at end of method ... */
    }
}

Finally, Option C is the best solution in my book, but my team doesn’t like this approach for whatever reason(s), hence I’m looking for a compromise.

Option C: single return, conditional blocks

public bool myMethod() {
    /* ... code ... */

    if(!thisCondition) {
        /* ... more code ... */
    }

    if(!thisCondition && !thatCondition) {
        /* ... even more code ... */
    }

    /* ... code that must run at end of method ... */
    return summaryCondition;
}

If you want to discuss multiple return statements, please do so in this question.

  • 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-05-13T16:15:21+00:00Added an answer on May 13, 2026 at 4:15 pm

    Exceptions should be exceptional so I don’t like option B if there are no other exceptions around(Note for downvoters – I don’t say that having finally is incorrect just that I prefer not to have it if there are no exceptions – if you have reasons please comment)

    If code is always needed how about refactoring into 2 functions

    public bool myMethod() {
        bool summaryCondition = myMethodWork();
        // do common code
        return summaryCondition;
    }
    
    private bool myMethodWork() {
       /* ... code ... */
    
        if(thisCondition) {
            return false;
        }
    
        /* ... more code ... */
    
        if(thatCondition) {
            return false;
        }
    
        /* ... even more code ... */
    
        return lastCondition;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that I only have one monitor, what's the best way to debug a
Given an integer x, how would you return an integer y that is lower
Well I'm looking for a function that reduce multiple space characters ' ' in
Given: Multiple clients with a Service.asmx page that has some web methods that I
My program uses wx.ProgressDialog to give feedback on a process that is in multiple
Given that I have a Gallery object and Photo object. Gallery has many Photos.
Given that indexing is so important as your data set increases in size, can
Given that Chrome and Safari use webkit has anyone yet found anything that renders
Given that a function a_method has been defined like def a_method(arg1, arg2): pass Starting
Given that Decimal.MaxValue = 79228162514264337593543950335m Why does the next line give me 7922816251426433759354395034M in

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.