patterncsharpMinor
Model interval inclusiveness in C#
Viewed 0 times
intervalmodelinclusiveness
Problem
This is my first attempt to model a day's time interval and the fact that the start and end are either inclusive or not:
Any suggestions for improvement?
public class UtcTimeOfDay
{
public int HourOfDay { get; set; }
public int MinuteOfDay { get; set; }
public int SecondOfDay { get; set; }
}
public class UtcTimeInterval
{
public UtcTimeOfDay StartUtcTimeOfDay { get; set; }
public UtcTimeOfDay EndUtcTimeOfDay { get; set; }
public bool StartInclusive { get; set; }
public bool EndInclusive { get; set; }
}Any suggestions for improvement?
Solution
Do you need
The disadvantage that using a raw
Now you get all the advantages of the rich functionality of
Regarding your original
This also reveals another problem: there's no guarantee that these values are valid (they could be negative, or above the maximum possible). This could be done with validation in a constructor, making the setters
The name
While you can use
Being able to do this so simply is another benefit of using a
So what I would do is remove the
Update
As a couple of people have noted in comments, using a
UtcTimeOfDay at all? DateTime already has useful methods for working with paramterized times.The disadvantage that using a raw
DateTime has is that you have no compile-time guarantee that you won't get an invalid DateTime (e.g. one with a Day component above 0). However, you can get the best of both worlds by having your class wrap a DateTime:public class UtcTimeOfDay
{
public readonly DateTime Value;
public UtcTimeOfDay(int hour, int minute, int second)
{
Value = new DateTime(0,0,0,hour,minute,second);
}
}Now you get all the advantages of the rich functionality of
DateTime, along with the guarantees provided by your original class.Regarding your original
UtcTimeOfDay, the names aren't great. Shouldn't MinuteOfDay be MinuteOfHour, and likewise shouldn't SecondOfDay be SecondOfMinute?This also reveals another problem: there's no guarantee that these values are valid (they could be negative, or above the maximum possible). This could be done with validation in a constructor, making the setters
private. The name
UtcTimeInterval doesn't make any reference to the fact that it's specifically for a time interval within a day. Why not UtcTimeOfDayInterval?While you can use
StartInclusive and EndInclusive, a normal technique to deal with exclusive time spans is simply to add or subtract a tick to your time. So for example, if the inclusive start time was startTimeInclusive, then if you want an exclusive version, that would be startTimeExclusive = startTimeInclusive.AddTicks(1);Being able to do this so simply is another benefit of using a
DateTime rather than trying to roll your own structure to represent the same thing.So what I would do is remove the
StartInclusive and EndInclusive properties, and do one of the following:- Just let whoever sets the start and end
UtcTimeOfDaytake responsibility for adding or subtracting a tick if they need to.
- Add a constructor with boolean
startInclusiveandendInclusiveparameters, as well as the start and endUtcTimeOfDays which itself does the tick adding/subtracting when needed.
Update
As a couple of people have noted in comments, using a
TimeSpan instead of a DateTime may be preferable, as it is more in fitting with the meaning of these structures. This makes the add/subtract tick logic slightly more verbose, but not prohibitively.Code Snippets
public class UtcTimeOfDay
{
public readonly DateTime Value;
public UtcTimeOfDay(int hour, int minute, int second)
{
Value = new DateTime(0,0,0,hour,minute,second);
}
}Context
StackExchange Code Review Q#87548, answer score: 5
Revisions (0)
No revisions yet.