Best practices for ranges

12 views
Skip to first unread message

Sean Farrow

unread,
Mar 26, 2020, 6:22:09 AM3/26/20
to noda...@googlegroups.com

Hi,

 

I’m in a situation where I need to ensure an instant is within range when validating arguments to a method.

 

What is the best practice for this:

To include both the minimum and maximum values, or have a half-open range excluding one value.

I’m looking to make this reusable using PostSharp, so would be interested as to what other people have done.

 

Thanks,

Sean

Jon Skeet

unread,
Mar 26, 2020, 6:24:10 AM3/26/20
to Noda Time
For instant ranges, I would definitely use "include minimum, exclude maximum" - which is exactly what the Interval type already encapsulates.

Date ranges are different - they'd typically include both ends.

--

---
You received this message because you are subscribed to the Google Groups "Noda Time" group.
To unsubscribe from this group and stop receiving emails from it, send an email to noda-time+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/noda-time/DB7PR05MB565965243807903FE839F4CCC6CF0%40DB7PR05MB5659.eurprd05.prod.outlook.com.

Sean Farrow

unread,
Apr 6, 2020, 9:26:53 AM4/6/20
to noda...@googlegroups.com

Hi,

 

Is there a reason the Instant.IsValid method and the Instant.MinValue/Instant.MaxValue are all internal?

It seems like these would be useful methods/fields to have exposed publicly.

Thanks,

Sean

Jon Skeet

unread,
Apr 6, 2020, 10:29:16 AM4/6/20
to Noda Time
The Instant.MaxValue and Instant.MinValue properties are public.
IsValid isn't relevant externally, as users never "see" invalid instants - invalid instants are outside the normal range, and are used for special internal scenarios.

Sean Farrow

unread,
Apr 7, 2020, 6:21:53 AM4/7/20
to noda...@googlegroups.com

Sorry, my fault, I meant Instant.BeforeMinValue.

Essentially, I need to create an instant that is completely invalid and is before the MinValue.

I would like not to have to use reflection. Is there anything in the testing assembly to do this?

Thanks,

Sean.

Jon Skeet

unread,
Apr 7, 2020, 6:45:31 AM4/7/20
to Noda Time
You can't do that, basically - and you shouldn't use reflection to get at those invalid values. Noda Time is not written to support that use case. Invalid instants are purely intended for internal convenience. If you want to indicate "either a valid Instant, or nothing" you should  use Nullable<Instant>.

(If you do decide to use reflection, I make absolutely no guarantees of consistent behaviour between releases. You're basically going out of documented, supported behaviour at that point.)

Jon


Reply all
Reply to author
Forward
0 new messages