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

  • Home
  • SEARCH
  • 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 6615637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:29:13+00:00 2026-05-25T20:29:13+00:00

Imagine a condintion should be true for a method to do its stuff. Which

  • 0

Imagine a condintion should be true for a method to do its stuff. Which block represents the best approach (performance related and readability), or if not what is your suggestion?!

private void method()
{
    if(!condition)
    {
     MessageBox.Show("ERROR!");
     return;
    }     
    else
    {
        //DO STUFF
    }
}

OR

private void method()
{
    if(condition)
    {
         //DO STUFF
    }     
    else
    {
         MessageBox.Show("ERROR!");
         return;
    }
}
  • 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-25T20:29:13+00:00Added an answer on May 25, 2026 at 8:29 pm

    Well, neither, as you wouldn’t use both else and return.

    So, you would either do:

    private void method() {
      if (!condition) {
        MessageBox.Show("ERROR!");
      } else {
        //DO STUFF
      }
    }
    

    or:

    private void method() {
      if (condition) {
        //DO STUFF
      } else {
        MessageBox.Show("ERROR!");
      }
    }
    

    or:

    private void method() {
      if (!condition) {
        MessageBox.Show("ERROR!");
        return
      }
      //DO STUFF
    }
    

    or:

    private void method() {
      if (condition) {
        //DO STUFF
        return;
      }
      MessageBox.Show("ERROR!");
    }
    

    Which you use depends mostly on what the code actually does. The code is seldom as simple as in the examples, so it matters what more the code will do.

    The first two have the advantage of having a single exit point, which often makes it easier to follow the code. You would usually put the shorter code first, as it’s easier to spot there than in an else after a larger code block.

    The third is often used to validate input before continuing with the main code, and you can easily have more than one validation:

    private void method() {
      if (!condition) {
        MessageBox.Show("ERROR!");
        return
      }
      if (!anotherCondition) {
        MessageBox.Show("ANOTHER ERROR!");
        return
      }
      //DO STUFF
    }
    

    The fourth is useful if you have several conditions that you don’t want to put in the same if statement:

    private void method() {
      if (condition) {
        var data = GetSomeData();
        if (data.IsValid) {
          var moreData = GetSomeMoreData();
          if (moreData.IsValid) {
            //DO STUFF
            return;
          }
        }
      }
      MessageBox.Show("ERROR!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine you have class A which has code which runs as method M. And
Imagine I want to draw a pyramid made of triangles. -Should I create a
Imagine I've got a database with lots of data, from which users can search.
Imagine you have one function that does stuff under one condition, eg. if an
Imagine a structure like this: struct my_struct { uint32_t refs ... } for which
Imagine a cube-shaped piece of Swiss cheese. We model the cheese through a 20x20x20
imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
Imagine an architecture composed of 2 Virtual Machines VM1 and VM2 hosted somewhere in
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do

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.