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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:19:45+00:00 2026-05-23T01:19:45+00:00

I was recently talking with a buddy about return values taking only a single

  • 0

I was recently talking with a buddy about return values taking only a single meaning. At my previous job, we worked with C++ and had typedef’ed wBOOL so that a 0 was wFALSE, and 1 was wTRUE. The architect said that we can also return 2, 3, 4… for more information, which I think is a horrible idea. If we expect wTRUE = 1 and wFALSE = 0 and wBOOL = {wTRUE, wFALSE}, returning anything else should be avoided… now, on to today’s C#.

I recently reviewed a piece of code where there were a collection of functions that determined if there was an error and returned the string back to the user:

private bool IsTestReady(out string errorMessage)
{
  bool isReady = true;
  errorMessage = string.Empty;
  if(FailureCondition1)
  {
    isReady = false;
    errorMessage = FailureMessage1;
  }
  else if(FailureCondition2)
  {
    isReady = false;
    errorMessage = FailureMessage2;
  }
  //... other conditions
  return isReady;
}

Then, to use these functions…

private enum Tests
{ TestA, TestB, TestC }
private void UpdateUI()
{
  string error = string.Empty;
  bool isTestReady;
  switch(this.runningTest) // which test are we running (TestA, TestB, or TestC)
  {
    case Tests.TestA:
      isTestReady = IsTestAReady(error);
      break;
    case Tests.TestB:
      isTestReady = IsTestBReady(error);
      break;
    case Tests.TestC:
      isTestReady = IsTestCReady(error);
      break;
  }
  runTestButton.Enabled = isTestReady;
  runTestLabel.Text = error;
}

I thought to separate these out into two methods:

private string GetTestAErrorMessage()
{
  //same as IsTestReady, but only returns the error string, no boolean stuffs
}

private bool IsTestAReady
{
  get{ return string.IsNullOrEmpty(GetTestAErrorMessage()); }
}

Does this violate the principal of not having a return value mean more than one thing? For instance, in this case, if there error message IsNullOrEmpty, then there is no error. I think that this does not violate that principal; my co-worked does. To me, it’s no different than this:

class Person
{
  public int Height {get;}
  public bool IsTall() { return Height > 10; }
}

Any thoughts or suggestions on a different approach to this issue? I think the out parameter is the worst of the solutions.

  • 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-23T01:19:46+00:00Added an answer on May 23, 2026 at 1:19 am

    I’m not a big fan of having the null or empty return value indicate that nothing is wrong. A better comparison than the one you gave is:

    class Person
    {
        public int Height {get;}
        public bool IsBorn() { return Height > 0; }
    }
    

    In .NET, it is common practice to use the “bool return with out parameter” pattern you see in your original method (see the various TryParse methods, for example). However, if you prefer, another solution would be to create a TestReadyCheck class with both the boolean and the string as properties. I’ve done something similar with the following class, and been quite happy with it.

    public class RequestFilterResult
    {
        public static readonly RequestFilterResult Allow = new RequestFilterResult(true, null);
        public static RequestFilterResult Deny(string reason) { return new RequestFilterResult(false, reason); }
        protected RequestFilterResult(bool allowRequest, string denialReason)
        {
            AllowRequest = allowRequest;
            DenialReason = denialReason;
        }
    
        public bool AllowRequest { get; private set; }
        public string DenialReason { get; private set; }
    }
    

    This allows for the following usage:

    public RequestFilterResult Filter(...)
    {
        if (FailureCondition1) return RequestFilterResult.Deny(FailureMessage1);
        if (FailureCondition2) return RequestFilterResult.Deny(FailureMessage2);
        return RequestFilterResult.Allow();
    }
    

    It’s concise, while enforcing that failure results provide a failure message, and success results don’t.

    On a side note, the structure of your switch statement feels like a code smell to me. You may want to consider ways to leverage polymorphism. Maybe make each test have its own class, with an IsTestReady method on it?

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

Sidebar

Related Questions

Recently I was talking to a co-worker about C++ and lamented that there was
Recently a friend and I were talking about securing stored procedure code in a
I have heard a lot of people talking recently about middleware , but what
Some friends of mine and I were talking recently about version control, and how
I'm talking about wrappers for third-party libraries. Until recently I was trying to provide
Recently at the office we have been talking about placing large files into our
Recently I was talking with a friend of mine who had started a C++
I recently heard the term hook while talking to some people about a program
I had a discussion recently about looking for a method to generate truly random
I was talking to somebody a recently who mentioned it was possible to store

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.