Bil*_*one 4 c# validation methods exception
我正在组建一个技术分析库,我希望其他人最终可以使用它,所以我想确保我验证数据进入我的方法并返回适当的东西.现在,如果验证失败,我会返回一个空值.抛出异常会更合适吗?如果其他开发人员可以使用此库,那么更好的做法是什么?以下是我目前的验证方式:
    /// <summary>
    /// Calculates the MACD (Moving Average Convergence Divergence) over n periods, where n is the number of elements in the input prices.
    /// </summary>
    /// <param name="InputValues">The numbers used for the MACD calculation. Index 0 must be the oldest, with each index afterwards one unit of time forward. There must more values present than what the SlowEMA calls for.</param>
    /// <param name="FastEMA">Optional: The smaller (faster) EMA line used in MACD. Default value is 12. Must be less than the SlowEMA.</param>
    /// <param name="SlowEMA">Optional: The larger (slower) EMA line used in MACD. Default value is 26. Must be less than the number of elements in InputValues.</param>
    /// <param name="SignalEMA">Optional: The EMA of the MACD line. Must be less than the FastEMA.</param>
    /// <returns>Returns the components of a MACD, which are the MACD line itself, the signal line, and a histogram number.</returns>
    public MACD CalculateMACD(decimal[] InputValues, decimal FastEMA = 12M, decimal SlowEMA = 26M, decimal SignalEMA = 9M)
    {
        MACD result;
        // validate that we have enough data to work with
        if (FastEMA >= SlowEMA) { return result; }
        if (SlowEMA >= InputValues.Count()) { return result; }
        if (SignalEMA >= FastEMA) { return result; }
        // Do MACD calculation here
        return result;
    }
.Net中有三种标准的参数异常:
它们都在构造函数上有重载,以允许您说明哪个参数是一个问题,并在抛出它们时更详细的消息.
通常的做法是使用这些.
编辑:
进一步为null(或空或其他)是可接受的,我仍然倾向于抛出异常,因为您不一定知道您的库的用户可以使用什么.如果他们不介意空值,他们总是可以捕获异常并在适当时处理它.
但是,如果您不抛出异常,那么您在向图书馆的消费者告知问题的能力方面受到限制.
| 归档时间: | 
 | 
| 查看次数: | 351 次 | 
| 最近记录: |