tags:

views:

133

answers:

3

I'm reading Fowler Clean Code book and I think that my code is a little messy, I want some suggestions:

I have a simple business requirement that is return the date of new execution of my Thread.

I've two class fields: _hour and _day.
If actual day is higher than my _day field I must return true, so I'll add a month to "executionDate"
If the day is the same, but the actual hour is higher than _hour I should return true too.
So I did this simple method:

private bool ScheduledDateGreaterThanCurrentDate (DateTime dataAtual) {

    if (dateActual.Day > _day) {
        return true;
    }

    if (dateActual.Day == _day && dateActual.Hour > _hour) {
        return true;
    }

    if (dateActual.Day == _day && dateActual.Hour == _hour) 
        if (dateActual.Minute>0 || dateActual.Second>0) 
            return true;

    return false;
}

I'm programming with TDD, so I know that the return is correct, but this is bad maintain code right?

+11  A: 
var compareDate = new DateTime(
    dateActual.Year,
    dateActual.Month,
    _day,
    _hour,
    0,
    0);
return dateActual> compareDate;
John Gietzen
Every tests passed, perfect. Thanks.
Luís Custódio
A: 

If you use the datetime object in .NET it will do all this logic for you.

Jay
+3  A: 

DateTime objects can be compared against one another.

For example, say my class has this field:

// Using DateTime.Today as an example
DateTime _date = DateTime.Today;

I can then do this instead of a method call

if (dataAtual > _date) {
    // logic here
}
R. Bemrose
Too simple, now I can see. I'll follow your clue.
Luís Custódio