0

anyone can give me an advice for improving this piece code, it give me a complexity of score of 10. There is something telling in back of my mind that it can be improve, but this is the best i can do. arggg

            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;
            }

I will appreciate for any help will come.

Edited by ZER09

2
Contributors
1
Reply
11
Views
2 Years
Discussion Span
Last Post by Traevel
0

Well, to reduce the complexity of this method you'll have to reduce the number of if-else statements. You could extract them to separate methods like isNull(object), isNumber(object) and isDecimal(object). Also you don't need an else since you're returning and thus never reaching the next if.

if (isNull(object)) {
    // message
    return false;
}

if (isNumber(object)) {
    // message
    return false;
}

if (isDecimal(object)) {
    // message
    return false;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.