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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:49:42+00:00 2026-05-24T05:49:42+00:00

Okay, this is a simple question, but I’d like some oppinions on the correct

  • 0

Okay, this is a simple question, but I’d like some oppinions on the correct practice here. I am not looking at this for performance concerns, because CPU’s are so powerful that this wouldn’t make any perceivable difference unless called without a looping contruct with thousands of iterations. I just want views on what is the accepted standard.

I have a method that bascially just does a check returns a boolean. However, there are numerous ways to implement this.

Here is how I would normally implement this.

    public bool CanUndo()
    {
        if (_nCurrentUndoIndex > 0)
            return true;
        else
            return false;
    }

However, it is often frowned upon to return from the middle of a method. The only time I normally do this is when performing a check on a form submission like this.

        if (String.IsNullOrEmpty(firstName.Text))
        {
            MessageBox.Show("Please enter a first name", "Incomplete");
            return;
        }

I consider that acceptable.

Back to the undo question, an alternative way to code it would be this.

    public bool CanUndo()
    {
        bool returnVal;
        if (_nCurrentUndoIndex > 0)
            returnVal = true;
        else
            returnVal = false;
        return returnVal;
    }

This however unncessarily allocates a variable and is more verbose code. Another option would be.

    public bool CanUndo()
    {
        bool returnVal = false;
        if (_nCurrentUndoIndex > 0)
            returnVal = true;
        return returnVal;
    }

This is more streamlined as it gets rid of the else. However, if the value is true is makes an unneccesary assignment by initializing it to false.

  • 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-24T05:49:43+00:00Added an answer on May 24, 2026 at 5:49 am
    public bool CanUndo () {
        return _nCurrentUndoIndex > 0;
    }
    

    Personally I don’t have a problem with returning from the middle of a method. It complicates cleanup code for C functions but with RAII that argument disappears.

    I prefer to exit as soon as is suitable otherwise you get

    if (x) {
       if (y) {
           if (z) {
              complete
           }
       }
    }
    

    rather than

    if (!x)
        return
    
    if (!y)
        return
    
    if (!z)
        return
    
    complete
    

    This way you avoid nesting, wide lines (horizontal screen space is expensive, vertical space is cheap) and you always know that if you’re still in a function then you’re not in an error path. Code which works well with this design also works well with exceptions, which is very important.

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

Sidebar

Related Questions

Okay this may be a simple question but I have yet to come with
Okay this is definitely a n00b question but here goes. The way I understand
Okay, this is (probably) a very simple question, but I am afraid I know
Okay, so this should be a simple question, but I'm fairly new at programming,
Okay, this is something that should be a simple matrix question, but my understanding
Okay this may seem too simple of a question but I've wasted enough time
Okay, simple template question. Say I define my template class something like this: template<typename
Okay, this might seem like a weird question, but bear with me. So I
Okay this question is very simple: I have a facebook page, and a website.
Okay, this is following on from my previous question reguarding performing a simple ajax

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.