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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:43:50+00:00 2026-05-14T20:43:50+00:00

Which is a better practice, generally speaking, and why? Under what circumstances would you

  • 0

Which is a better practice, generally speaking, and why? Under what circumstances would you change your mind?

function foo1(int x) {
  int result;
  if (x > 5) {
    result = 2;
  } else {
    result = 7;
  }
  return result;
}

OR

function foo2(int x) {
  if (x > 5) {
    return 2;
  } else {
    return 7;
  }
}
  • 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-14T20:43:51+00:00Added an answer on May 14, 2026 at 8:43 pm

    I prefer the second form where you just return immediately. The only considerations I’m aware of are as follows.
    Benefits:

    • Shorter code
    • Easier to follow
    • Fewer variables (part of easier to follow, I guess)

    Risks:

    • May skip clean-up processing that occurs after the return

    I don’t worry about the risk much because that risk kind of exists anyway if you inadvertently nest the clean-up code inside a conditional block of some sort as you try to manage the various paths of execution retaining the return value until the end. I think the best bet is to keep the code simpler and easier to follow to avoid that kind of risk. I think that risk is also significantly helped by the coding structures available in modern languages such as “Using” and “Try/Finally”. I try to always use those for clean-up tasks now instead of simply putting the code at the end of the block.

    I do make an exception, however, when other code wants to access the pending return value as a variable anyway (to add it to a cache for example, so that the result can be returned more quickly next time).

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

Sidebar

Related Questions

Give me some of your thoughts on which is a better coding practice/makes more
I would like to know which is considered more correct or better practice: Part
Is there any considerations to determine which is better practice for creating an object
In C, which is the better practice when it comes to freeing memory returned
Which of these two statements is faster/better practice? myList.Where(x => { bool itemOne= x.ItemOne
In general, is it a better practice to write a new NamedQuery which selects
Which is better practice; manipulating properties with accessors by @property or self.property ?
This is a general, best-practice question. Which of the following try-except examples is better
Which option is better from Best practices point of view and from performance point
Far as best practices are concerned, which is better: public void SomeMethod(string str) {

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.