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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:48:31+00:00 2026-05-15T03:48:31+00:00

I was looking at some code I’ve inherited and I couldn’t decided if I

  • 0

I was looking at some code I’ve inherited and I couldn’t decided if I like a bit of code.

Basically, there is a method that looks like the following:

bool Connect(connection parameters){...}

It returns true if it connects successfully, false otherwise.

I’ve written code like that in the past, but now, when I see this method I don’t like it for a number of reasons.

  1. Its easy to write code that just ignores the returned value, or not realize it returns a value.

  2. There is no way to return an error message.

  3. Checking the return of the method doesn’t really look nice:

    if (!Connect(…)){….}

I could rewrite code to throw an exception when it doesn’t successfully connect, but I don’t consider that an exceptional situation. Instead I’m thinking of refactoring the code as follows:

void Connect(Connection Parameters, out bool successful, out string errorMessage){...}

I like that other developers have to provide the success and error strings so they know the method has error conditions and I can know return a message

Anyone have any thoughts on the matter?

Thanks
-Matt

  • 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-15T03:48:31+00:00Added an answer on May 15, 2026 at 3:48 am

    I have only an opinion, so take it for what its worth.

    A method named like this, “Connect”, is an order. It’s like giving it to a soldier, “Jump” or “Shoot”. You don’t expect the soldier to report back unless he’s unable to complete the order, and that would be a rare happening.

    As such, I have the tendency to not have a return value for such methods, but if there is a chance that under regular usage of the method, there will be failures, then I build a second method, named TryXYZ, returning a bool, and if necessary providing me with the results of whatever XYZ is as out parameters.

    This follows the standard set forth by the Parse methods of various numeric types in the .NET BCL.

    So in your case, I would probably have:

    void Connect(connection parameters);
    bool TryConnect(connection parameters, out status);
    

    The nice thing is that if you build the TryConnect method properly, Connect becomes really easy.

    Example:

    public bool TryConnect(string connectionString, out ConnectionStatus status)
    {
        ... try to connect
        ... set status, and return true/false
    }
    
    public void Connect(string connectionString)
    {
        ConnectionStatus status;
        if (!TryConnect(connectionString, out status))
            switch (status)
            {
                case ConnectionStatus.HostNotFound:
                    throw new HostNameNotFoundException();
                ...
            }
    }
    

    I don’t expect my orders to not complete, but in the sense they might, I want to be explicit about it.

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

Sidebar

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.