You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because I've recently been dealing with Duration a lot, and having to check whether positive or positive or zero, I created a discussion ( #1217 ) on whether to create our own KiwiDurations utility. Admittedly, it would contain some pretty trivial methods. But after writing:
if (!duration.isNegative() && !duration.isZero()) { ... }
and even:
if (!duration.isNegative()) { ... }
several times, I wanted this instead:
if (duration.isPositive()) { ... }
and
if (duration.isPositiveOrZero()) { ... }
Why? First, having to write a compound boolean expression that negates each individual expression is annoying, especially when you do it many times (or even twice). Second, compound expressions are more difficult to read when skimming code, especially when each one has a ! in front of it (it's easy to miss them).
Since this library isn't Kotlin or a language supporting extension methods, I clearly can't modify the JDK's Duration class. So, I guess we'll just have to do with:
if (KiwiDurations.isPositive(duration)) { ... }
and
if (KiwiDurations.isPositiveOrZero(duration)) { ... }
With static imports it's nicer - just isPositive(duration).
While we're at it, might as well support the other missing option:
if (KiwiDurations.isNegativeOrZero(duration)) { ... }
So, that's three simple, but useful and easier to read in my opinion, methods:
isPositive(Duration)
isPositiveOrZero(Duration)
isNegativeOrZero(Duration)
The text was updated successfully, but these errors were encountered:
Java's Duration contains (instance) methods isNegative() and
isZero(), but nothing else. Add convenience methods that allow
for one-liner checks of Duration instances whether they are
positive, positive or zero, or negative or zero.
The initial KiwiDurations contains the following methods:
* isPositive
* isPositiveOrZero
* isNegativeOrZero
Closes#1221
Because I've recently been dealing with
Duration
a lot, and having to check whether positive or positive or zero, I created a discussion ( #1217 ) on whether to create our ownKiwiDurations
utility. Admittedly, it would contain some pretty trivial methods. But after writing:and even:
several times, I wanted this instead:
and
Why? First, having to write a compound boolean expression that negates each individual expression is annoying, especially when you do it many times (or even twice). Second, compound expressions are more difficult to read when skimming code, especially when each one has a
!
in front of it (it's easy to miss them).Since this library isn't Kotlin or a language supporting extension methods, I clearly can't modify the JDK's
Duration
class. So, I guess we'll just have to do with:and
With static imports it's nicer - just
isPositive(duration)
.While we're at it, might as well support the other missing option:
So, that's three simple, but useful and easier to read in my opinion, methods:
isPositive(Duration)
isPositiveOrZero(Duration)
isNegativeOrZero(Duration)
The text was updated successfully, but these errors were encountered: