0

I've been messing around with this for a long time and I couldn't find out what code path doesn't return a value. I'm trying to check if an entered date is later than the current date. I'd thank you if you can help me out with this problem. (Date is a class and the brackets are me testing it out with an example.)

public bool IsLater(Date d)
{                              //Date d = 2020/4/25  Date = 2020/4/21
    if (d.year > year) //false
    {
        return true;
    }
    if (d.year < year) //false
    {
        return false;
    }
    if (d.year == year)//true
    {
        if (d.month > month)//false
        {
            return true;
        }
        if (d.month < month)//false
        {
            return false;
        }
        if (d.month == month)//true
        {
            if (d.day > day)//true
            {
                return true;//ok
            }
            if (d.day < day)
            {
                return false;
            }
            else if (d.day == day)
                return false;
        }
    }
}
4
  • What datatypes are your year, month and day fields/properties? If they are nullable, then the paths do not hit. A solution is to remove the check for equal year and month, since before that, you check for less-than and greater-than. So those is-equal-to if's are redundant. Commented Apr 10, 2020 at 7:43
  • 1
    why not return d.Date > new DateTime(year, month, day);? Commented Apr 10, 2020 at 7:45
  • This has been answered in a question that just came up Commented Apr 10, 2020 at 7:45
  • @Maarten right, my bad, i'm just a beginner so I make some rookie mistakes :) Commented Apr 10, 2020 at 7:53

3 Answers 3

2

Personally, I would rewrite it to this. IMHO easier to read, and no 'not all paths ...' warning.

return
    d.year > year ||
    (d.year == year && d.month > month) ||
    (d.year == year && d.month == month && d.day > day)
    ;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, can you explain in a couple words what every step does? appreciate your time
1

I would use the DateTime class instead:

public bool IsLater(Date d)
{   
    DateTime a = new DateTimte (year, month, day);
    DateTime b = new DateTimte (d.year, d.month, d.day);
    return b > a;
}

Comments

0

First, you can let .Net make the work for you: why reinvent the wheel? Comparing dates is just > operator:

// Later: d is more than (year, month, day)
public bool IsLater(DateTime d) => d.Date > new DateTime(year, month, day);

If you insist on keeping your current routine, let compiler know that ifs are combined: turn if into else if and final if in each group into else

public bool IsLater(Date d)
{                              
    if (d.year > year) 
        return true;
    else if (d.year < year) 
        return false;
    else // we have nothing else but equal years
    {
        if (d.month > month)
            return true;
        else if (d.month < month)
            return false;
        else // nothing else but equal months
        {
            if (d.day > day)
                return true;
            else if (d.day < day)
                return false;
            else 
                return false;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.