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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:48:02+00:00 2026-05-16T11:48:02+00:00

Which method of returning a bool from a switch statement is preferable? I understand

  • 0

Which method of returning a bool from a switch statement is preferable? I understand this may be subjective, however I feel it is important to our profession to get input on best practices :).

public bool foo(param)
{
    switch (param)
    {
        case 1:
            if (something)
            {
                return true;
            }

            return false;    
        default:
            return false;
     }
}

– OR –

public bool foo(param)
{
    bool flag = false;

    switch (param)
    {
        case 1:
            if (something)
            {
                flag = true;
            }
            break;
        default:
            break;
     }

    return flag;
}
  • 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-16T11:48:03+00:00Added an answer on May 16, 2026 at 11:48 am

    The difference here is between single point of return and multiple points of return.

    Generally, single point of return code tends to do a lot of bookkeeping (temporary variables, checking of those variables in loops), and can get hairy as the logic gets more complex. I’ve seen code that goes to great lengths to use a while (flag) ... flag = false; pattern instead of a while (true) ... break; pattern, and it is not fun to read.

    I prefer multiple points of return since they return as early as possible (no extra work done) and don’t require any locals to help keep track of the current return value. Also, I don’t find them any harder to read than single point of return code.

    There’s a good disucssion of this on c2 (SingleFunctionExitPoint).

    I’ve found that keeping methods as “functional” as possible is a good thing (I say “functional” here because I’m typically doing C# which, outside of LINQ, is not considered functional). By “functional”, I mean that I try to avoid mutating state as much as possible. Introducing more state in the form of locals or members makes things more difficult to understand since you now have to take into consideration their values and you have to consider how those variables can be set (from another method, from another class, …).

    In general, state is evil, but sometimes it’s a necessary evil.

    This particular issue has some interesting conversation here on SO as well.

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

Sidebar

Related Questions

I have this method called MatchNodes: IEnumerable<bool> MatchNodes<T>(T n1, T n2) Which basically gets
Having an assembly which I cannot modify (vendor-supplied) which have a method returning an
Let's say I have a WCF service which has a method returning object Person.
Which method is very effective and simple to code to attain this functionality ?
Which method can be used to read one line at a time from a
This is similar to : .NET: bool vs enum as a method parameter but
When constructing an API response, which method is better for (manually) returning the status
I have the username entered from a form Which method to use if that
If I have a method which establishes a database connection, how could this method
I have a WCF service which has one method returning a stream. [ServiceContract] public

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.