我正在整理一个技术分析库,我希望其他人最终可以使用它,所以我想确保我验证进入我的方法的数据并返回适当的东西。现在,如果验证失败,我让它返回一个空白值。抛出异常会更合适吗?如果其他开发人员可以使用这个库,更好的做法是什么?这是我目前验证的方式:
/// <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;
}