Best practices for ranges

Visto 16 veces
Saltar al primer mensaje no leído

Sean Farrow

no leída,
26 mar 2020, 6:22:0926/3/20
a 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

no leída,
26 mar 2020, 6:24:1026/3/20
a 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

no leída,
6 abr 2020, 9:26:536/4/20
a 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

no leída,
6 abr 2020, 10:29:166/4/20
a 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

no leída,
7 abr 2020, 6:21:537/4/20
a 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

no leída,
7 abr 2020, 6:45:317/4/20
a 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


Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos