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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:43:31+00:00 2026-05-21T16:43:31+00:00

I have faced difficulties trying to return a result combined with the set of

  • 0

I have faced difficulties trying to return a result combined with the set of errors from function call.

I have started from the following approach

List<String> errors;
bool result = Obj.GetResult(out errors, int id);

The second step was to introduce new class

public class OperationResult
{
  public bool Result {get;set;}
  public List<string> Errors {get;set;}

}

But then the dirty code started to appear inside the GetResult method.

For example

public OperationResult GetResult(int id)
{
  if (id == 0)
  {
    return new OperationResult { Result = false, Errors = {"Error"}};
  }

  if (id < 400)
  {
    var result = new OperationResult { Result = false, Errors = {"Error"}};
    if (id >200)
       result.Errors.Add("Error");
    return result;
  }
}

Then I’ve started to worry not allowing result user to edit the result they get.

I have extracted interface from the OpearationResult class which was able only to read data.

Now I’m want to add result builder class…

And at this point I’ve started to thinking that I’m doing something wrong. Trying to reinvent the bicycle, or just creating problems out of the air.

Please give me an advice, how to handle all this stuff.
I am also curious how this is handled in the functional programming languages. (I mean immutability)

  • 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-21T16:43:32+00:00Added an answer on May 21, 2026 at 4:43 pm

    This might be a time to start throwing exceptions.
    In his book Clean Code, Robert Martin talks about the confusion and lack of readable code when you do something like:

    CustomResult result = myUser.CanLogOn();
    

    Because it would appear from reading the code that CanLogOn should simply return a bool indicating if the user is allowed to log on, but now it’s getting a custom result object that has error codes etc. This will cause you to further pollute your code with things like

    if(result.Result)
       LogOn();
    

    instead of

    if(myUser.CanLogOn())
       LogOn();
    

    or even better

    myUser.LogOn() 
    

    and let it decide if it can or not.

    This is a simplified example, because I’d assume apart from a DB exception there isn’t a lot of possible errors that can occur in a CanLogOn() method.

    While you shouldn’t use exceptions for normal flow, they are there, in part, to prevent this pattern of output values, and error codes on the return, and needing to know the difference between a return code of 200 and 402 and -134.

    It will make your code cleaner, easier to read, and might prompt you to examine if you really need all those exceptions, and if so, is this method the best place to throw them from.

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

Sidebar

Related Questions

i have faced this problem couple of days ago, while trying to import an
I have faced this problem quite often during the last couple of months, during
I need to convert several Java classes to C#, but I have faced few
We have recently been faced with the problem of porting our C++ framework to
I am currently faced with a difficult sorting problem. I have a collection of
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have faced a very strange situation here. I am accessing database (MDB) through
I have faced with a situation in VB.NET and C# (.NET2) with the visibility
I have faced this question in my Interview as well. I do have many

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.