HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Refactoring IEnumerable<ValidationResult>

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
refactoringvalidationresultienumerable

Problem

I have the below code which increases the code complexity. Any way to refactor it?

I am trying to refactor the below code so that the CCM can be reduced for the code. i.e code complexity matrix.

//// TODO: Needs refactoring
    public IEnumerable Validate(ValidationContext validationContext)
    {
        var memberNames = new List();

        if (this.DateOfBirth == DateTime.MaxValue)
        {
            memberNames.Add("DateOfBirth");
            yield return new ValidationResult("Invalid date, please enter your date of birth", memberNames);
        }
        else if (this.DateOfBirth == DateTime.MinValue)
        {
            memberNames.Add("DateOfBirth");
            yield return new ValidationResult("Invalid date, please enter your date of birth", memberNames);
        }

        if (!this.Age.HasValue)
        {
            memberNames.Add("DateOfBirth");
            yield return new ValidationResult("Invalid date, please enter your date of birth", memberNames);
        }
        else
        {
            if (this.Age  75)
            {
                memberNames.Add("DateOfBirth");
                yield return new ValidationResult("Age must be between 18 and 75 years", memberNames);
            }
        }

        if (!this.Gender.HasValue)
        {
            memberNames.Add("Gender");
            yield return new ValidationResult("This field is required", memberNames);
        }

        if (!(this.Age >= 60))
        {
            yield break;
        }

        // similarly many more validation checks - if-else conditions
    }

Solution

I don't know the other conditions but I would do it like this:

public IEnumerable Validate(ValidationContext validationContext)
{
    var memberNames = new List();

    bool isValidDateOfBirth = DateOfBirth > DateTime.MinValue && DateOfBirth = 18 && Age <= 75;
    if (!isValidAge)
    {
        memberNames.Add("DateOfBirth");
        yield return new ValidationResult("Age must be between 18 and 75 years", memberNames);
    }

    bool isValidGender = Gender.HasValue;
    if (!isValidGender)
    {
        memberNames.Add("Gender");
        yield return new ValidationResult("This field is required", memberNames);
    }
}

Code Snippets

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    var memberNames = new List<string>();

    bool isValidDateOfBirth = DateOfBirth > DateTime.MinValue && DateOfBirth < DateTime.MaxValue && Age.HasValue;
    if (!isValidDateOfBirth)
    {
        memberNames.Add("DateOfBirth");
        yield return new ValidationResult("Invalid date, please enter your date of birth", memberNames);
    }

    bool isValidAge = Age.HasValue && Age.Value >= 18 && Age <= 75;
    if (!isValidAge)
    {
        memberNames.Add("DateOfBirth");
        yield return new ValidationResult("Age must be between 18 and 75 years", memberNames);
    }

    bool isValidGender = Gender.HasValue;
    if (!isValidGender)
    {
        memberNames.Add("Gender");
        yield return new ValidationResult("This field is required", memberNames);
    }
}

Context

StackExchange Code Review Q#71664, answer score: 2

Revisions (0)

No revisions yet.