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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:40:54+00:00 2026-06-11T16:40:54+00:00

Should I save a result from a switch statement before returning it? Or should

  • 0

Should I save a result from a switch statement before returning it? Or should I return the value in the switch when I get it? Is there one way over another that is better style?

With a temporary variable:

      public double xMax {
      get {
           double val = 0.00f;
           switch(x_option) {
                case 0:
                     val = priMax;
                     break;
                case 1:
                     val = rfMax;
                     break;
                case 2:
                     val = pwMax;
                     break;
           }

           return val;
      }
 }

With return statement:

 public double xMax {
      get {
           double val = 0.00f;
           switch(x_option) {
                case 0:
                     return priMax;
                case 1:
                     return rfMax;
                case 2:
                     return pwMax;
           }
      }
 }

Is there a performance difference and/or cleanup?

  • 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-11T16:40:55+00:00Added an answer on June 11, 2026 at 4:40 pm

    Personally I prefer the second form. It makes it immediately clear that as soon as you’ve got to the return statement, you’re done. With the extra variable version, you have to look through the rest of the code to see if anything else will happen.

    The dogma around a single point of return made sense in C, where you’d want to make sure you did all your manual clean-up etc – but in a world where you have to consider the possibility of exceptions anyway, and the garbage collector handles most of the cleanup, with try/finally handling the rest, I find there are a lot of cases where insisting on a single exit point makes the code significantly harder to read – particularly around cases where you can determine the result right at the start of the method (e.g. “if the input string is empty, the result is always going to be 0 – so just return it there).

    EDIT: Just to be clear, in your situation I don’t think it makes much difference. But this is the kind of code where single point of return gets messy:

     public int DoSomething(string input1, string input2)
     {
         // First simple case
         if (input1 == null || input2 == null)
         {
             return -1;
         }
    
         // Second simple case
         int totalLength = input1.Length + input2.Length;
         if (totalLength < 10)
         {
             return totalLength;
         }
    
         // Imagine lots of lines here, using input1, input2 and totalLength
         // ...
         return someComplicatedResult;
     }
    

    With a single point of return, this would become:

     public int DoSomething(string input1, string input2)
     {
         int ret;
    
         // First simple case
         if (input1 == null || input2 == null)
         {
    
             ret = -1;
         }
         else
         {
             // Second simple case
             int totalLength = input1.Length + input2.Length;
             if (totalLength < 10)
             {
                 ret = totalLength;
             }
             else
             {
                 // Imagine lots of lines here, using input1, input2 and totalLength
                 // ...
                 ret = someComplicatedResult;
             }
         }
         return ret;
     }
    

    I would definitely rather read the first form than the second:

    • More nesting generally makes code harder to understand. It’s hard to remember where you’ve got to
    • In the first form, you can clearly tell – without reading any more code – that if you get to the base cases, you’re done. There’s not going to be any side effect after the complicated code; we’re just going to return. In the second code, you have to mentally skip over the “else” blocks to work out what’s happening. You’re looking over more code to work out the path.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey there i have this code that should save a file from sql server
I want to know if there's a way to filter the result from ORM.
How should one deal with an MVC controller returning a null ViewResult? As an
I have 20 different methods and use datareader to read and get result from
When I press the Save button I return from my Controller this: return Json(new
I have as a result from a dynamic query a table with only one
Is there a way to save the querystring ids after the page load? the
I have this program in Python which should save text files to a folder
I am looking for where I should save my app stuff, and after a
Yesterday I asked the question on how I should save my files. After some

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.