For example, you want to check in Kotlin if an Instant is in the range of start and end of other two Instants you can simply do the following:
import java.time.Instant
val start = Instant.parse("2000-10-23T00:00:00Z");
val end = Instant.parse("2020-10-23T23:59:59Z");
val toTest = Instant.parse("2010-10-24T00:00:00Z");
if(toTest in start..end) println("Yes") else println("No")
which is in my opinion pretty simple to understand
And you could also write in the standard way the following to check the same:
import java.time.Instant
val start = Instant.parse("2000-10-23T00:00:00Z");
val end = Instant.parse("2020-10-23T23:59:59Z");
val toTest = Instant.parse("2020-10-24T00:00:00Z");
val result = start <= toTest && toTest <= end
if(result) println("Yes") else println("No")
My question is now is there a difference in consumed resources or in additional overhead with the range solution.
Is it better for some reason to use the standard way for checking e.g. Instants within a range?
Is there a difference in consumed resources or in additional overhead with the range solution?
The range solution actually creates a ClosedRange<Instant>
object, because of the ..
operator. Then in
does exactly the same thing as the second code snippet - start <= toTest && toTest <= end
.
So the ranges solution does do something extra, but in almost all the cases, this little bit of extra work won't be the main thing that slows your application down. It's not going to matter much.
Is it better for some reason to use the standard way for checking
Yes because it is more concise and readable. It more clearly shows your intent by saying "if this instant is in this range" rather than "if this instant is less than this instant and greater than that instant".