我需要帮助改进我的验证码
I need help improving my validation code
评估时,我的代码的圈复杂度得分为 10。什么是圈复杂度,谁能提供降低以下代码复杂度的建议:
public override bool Validate(Control control, object value)
{
if (value == null && !_IsAllowNull)
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
else
{
if (value.ToString().Contains("."))
{
ErrorText = "Decimal value is not allowed";
return false;
}
else
{
if (!value.IsNumber())
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
else
{
if (value.ToInt() < _minValue || value.ToInt() > _maxValue)
{
ErrorText = "Value should not be greater than " + _maxValue + " or less than " + _minValue;
return false;
}
}
}
}
return true;
}
谢谢。
由于您的验证检查返回 false,因此您不需要 else
部分,因此代码可以继续下一个检查:
public override bool Validate(Control control, object value)
{
if (value == null && !_IsAllowNull)
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
if (value.ToString().Contains("."))
{
ErrorText = "Decimal value is not allowed";
return false;
}
if (!value.IsNumber())
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
if (value.ToInt() < _minValue || value.ToInt() > _maxValue)
{
ErrorText = "Value should not be greater than " + _maxValue + " or less than " + _minValue;
return false;
}
return true;
}
public override bool Validate(Control control, object value)
{
if ((value == null && !_IsAllowNull) || !value.IsNumber())
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
if (value.ToString().Contains("."))
{
ErrorText = "Decimal value is not allowed";
return false;
}
if (value.ToInt() < _minValue || value.ToInt() > _maxValue)
{
ErrorText = "Value should not be greater than " + _maxValue + " or less than " + _minValue;
return false;
}
return true;
}
有了这个,你的方法复杂度分数就达到了 2
public Validation()
{
_Validations = new List<Action<object>>
{
ValidateNull,
ValidateDecimal,
ValidateIsNumber,
ValidateRange,
};
}
public bool Validate(Control control, object value)
{
try
{
_Validations.ForEach(c => c(value));
return true;
}
catch (Exception e)
{
ErrorText = e.Message;
return false;
}
}
private void ValidateNull(object value)
{
if (value == null && !_IsAllowNull)
throw new Exception("Please provided valid number without a decimal point.");
}
private void ValidateRange(object value)
{
if (value.ToInt() < _minValue || value.ToInt() > _maxValue)
throw new Exception("Value should not be greater than " + _maxValue + " or less than " + _minValue);
}
private static void ValidateIsNumber(object value)
{
if (!value.IsNumber())
throw new Exception("Please provided valid number without a decimal point.");
}
private static void ValidateDecimal(object value)
{
if (value.ToString().Contains("."))
throw new Exception("Decimal value is not allowed");
}
评估时,我的代码的圈复杂度得分为 10。什么是圈复杂度,谁能提供降低以下代码复杂度的建议:
public override bool Validate(Control control, object value)
{
if (value == null && !_IsAllowNull)
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
else
{
if (value.ToString().Contains("."))
{
ErrorText = "Decimal value is not allowed";
return false;
}
else
{
if (!value.IsNumber())
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
else
{
if (value.ToInt() < _minValue || value.ToInt() > _maxValue)
{
ErrorText = "Value should not be greater than " + _maxValue + " or less than " + _minValue;
return false;
}
}
}
}
return true;
}
谢谢。
由于您的验证检查返回 false,因此您不需要 else
部分,因此代码可以继续下一个检查:
public override bool Validate(Control control, object value)
{
if (value == null && !_IsAllowNull)
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
if (value.ToString().Contains("."))
{
ErrorText = "Decimal value is not allowed";
return false;
}
if (!value.IsNumber())
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
if (value.ToInt() < _minValue || value.ToInt() > _maxValue)
{
ErrorText = "Value should not be greater than " + _maxValue + " or less than " + _minValue;
return false;
}
return true;
}
public override bool Validate(Control control, object value)
{
if ((value == null && !_IsAllowNull) || !value.IsNumber())
{
ErrorText = "Please provided valid number without a decimal point.";
return false;
}
if (value.ToString().Contains("."))
{
ErrorText = "Decimal value is not allowed";
return false;
}
if (value.ToInt() < _minValue || value.ToInt() > _maxValue)
{
ErrorText = "Value should not be greater than " + _maxValue + " or less than " + _minValue;
return false;
}
return true;
}
有了这个,你的方法复杂度分数就达到了 2
public Validation()
{
_Validations = new List<Action<object>>
{
ValidateNull,
ValidateDecimal,
ValidateIsNumber,
ValidateRange,
};
}
public bool Validate(Control control, object value)
{
try
{
_Validations.ForEach(c => c(value));
return true;
}
catch (Exception e)
{
ErrorText = e.Message;
return false;
}
}
private void ValidateNull(object value)
{
if (value == null && !_IsAllowNull)
throw new Exception("Please provided valid number without a decimal point.");
}
private void ValidateRange(object value)
{
if (value.ToInt() < _minValue || value.ToInt() > _maxValue)
throw new Exception("Value should not be greater than " + _maxValue + " or less than " + _minValue);
}
private static void ValidateIsNumber(object value)
{
if (!value.IsNumber())
throw new Exception("Please provided valid number without a decimal point.");
}
private static void ValidateDecimal(object value)
{
if (value.ToString().Contains("."))
throw new Exception("Decimal value is not allowed");
}