Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have only an opinion, so take it for what its worth.</p> <p>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.</p> <p>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.</p> <p>This follows the standard set forth by the Parse methods of various numeric types in the .NET BCL.</p> <p>So in your case, I would probably have:</p> <pre><code>void Connect(connection parameters); bool TryConnect(connection parameters, out status); </code></pre> <p>The nice thing is that if you build the TryConnect method properly, Connect becomes really easy.</p> <p>Example:</p> <pre><code>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(); ... } } </code></pre> <p>I don't expect my orders to not complete, but in the sense they might, I want to be explicit about it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload