How to indicate that a method was unsuccessful

前端 未结 16 1443
走了就别回头了
走了就别回头了 2020-12-31 08:03

I have several similar methods, say eg. CalculatePoint(...) and CalculateListOfPoints(...). Occasionally, they may not succeed, and need to indicate this to the caller. For

相关标签:
16条回答
  • 2020-12-31 08:13

    The model I've used is the same one MS uses with the TryParse methods of various classes.

    Your original code:
    public Point CalculatePoint(... out Boolean boSuccess);
    public List CalculateListOfPoints(... out Boolean boSuccess);

    Would turn into public bool CalculatePoint(... out (or ref) Point CalculatedValue);
    public bool CalculateListOfPoints(... out (or ref) List CalculatedValues);

    Basically you make the success/failure the return value.

    0 讨论(0)
  • 2020-12-31 08:15

    The bool TrySomething() is at least a practice, which works ok for .net's parse methods, but I don't think I like it in general.

    Throwing an exception is often a good thing, though it should not be used for situations you would expect to happen in many normal situations, and it has an associated performance cost.

    Returning null when possible is in most cases ok, when you don't want an exception.

    However - your approach is a bit procedural - what about creating something like a PointCalculator class - taking the required data as parameters in the constructor? Then you call CalculatePoint on it, and access the result through properties (separate properties for Point and for Success).

    0 讨论(0)
  • 2020-12-31 08:16

    A pattern that I'm experimenting with is returning a Maybe. It has the semantics of the TryParse pattern, but a similar signature to the null-return-on-error pattern.

    I'm not yet convinced one way or the other, but I offer it for your collective consideration. It does have the benefit of not requiring a variable to defined before the method call to hold the out parameter at the call site of the method. It could also be extended with an Errors or Messages collection to indicate the reason for the failure.

    The Maybe class looks something like this:

    /// <summary>
    /// Represents the return value from an operation that might fail
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public struct Maybe<T>
    {
        T _value;
        bool _hasValue;
    
    
        public Maybe(T value)
        {
            _value = value;
            _hasValue = true;
        }
    
        public Maybe()
        {
            _hasValue = false;
            _value = default(T);
        }
    
    
        public bool Success
        {
            get { return _hasValue; }
        }
    
    
        public T Value
        {
            get 
                { // could throw an exception if _hasValue is false
                  return _value; 
                }
        }
    }
    
    0 讨论(0)
  • 2020-12-31 08:17

    Personally, I think I'd use the same idea as TryParse() : using an out parameter to output the real value, and returning a boolean indicating whether the call was successful or not

    public bool CalculatePoint(... out Point result);

    I am not a fan of using exception for "normal" behaviors (if you expect the function not to work for some entries).

    0 讨论(0)
  • 2020-12-31 08:17

    It mostly depends on the behavior of your methods and their usage.

    If failure is common and non-critical, then have your methods return a boolean indicating their success and use an out parameter to convey the result. Looking up a key in a hash, attempting to read data on a non-blocking socket when no data is available, all these examples fall in that category.

    If failure is unexpected, return directly the result and convey errors with exceptions. Opening a file read-only, connecting to a TCP server, are good candidates.

    And sometimes both ways make sense...

    0 讨论(0)
  • 2020-12-31 08:17

    You don't want to be throwing exceptions when there is something expected happening, as @Kevin stated exceptions are for exceptional cases.

    You should return something that is expected for the 'failure', generally null is my choice of bad return.

    The documentation for your method should inform the users of what to expect when the data does not compute.

    0 讨论(0)
提交回复
热议问题