Poll: Would you find these methods helpful in the CodeIgniter 4 Time class? You do not have permission to vote in this poll. |
|||
Yes, this improves readability and simplifies code. | 7 | 87.50% | |
No, the current methods are sufficient. | 1 | 12.50% | |
Total | 8 vote(s) | 100% |
* You voted for this item. | [Show Results] |
Proposal: Add isPast() and isFuture() to CodeIgniter 4 Time Class |
Hello CodeIgniter Community! ?
I propose adding two new methods, isPast() and isFuture(), to the Time class. These methods would improve code readability, reduce complexity, and enhance efficiency when working with dates and times. Instead of: PHP Code: if ($time->isBefore(Time::now())) { /* Time is in the past */ } We can write: PHP Code: if ($time->isPast()) { /* Time is in the past */ } And instead of: PHP Code: if ($time->isAfter(Time::now())) { /* Time is in the future */ } We can write: PHP Code: if ($time->isFuture()) { /* Time is in the future */ } Practical Use Cases: Checking if a discount code has expired: PHP Code: if ($expiryDate->isPast()) { Determining if an event is upcoming: PHP Code: if ($eventDate->isFuture()) { Checking if a message should be sent: PHP Code: if ($sendTime->isFuture()) { Verifying user subscription status: PHP Code: if ($subscriptionEnd->isPast()) { What Do You Think?
I would argue that it doesn't make the code any more readable. Requiring a dev to input Time::now() makes it obvious the method is used for a temporal comparison.
It certainly qualifies as a convenience/efficiency method but its use case is already covered by the more general isBefore, isAfter. If someone specifically desired these methods coudn't they create their own Time class that uses TimeTrait and add these methods? Am I missing something that makes these methods necessary? Wouldn't it also be possible to refactor isBefore and isAfter to check if the passed time is null and assume Time::now() so that isPast and isFuture aren't required at all? The point of this is to say that isBefore() and isAfter() is no less confusing than isPast() or isFuture() without a parameter. You're trying to use the idea of isPresent in your mind to indicate that Time::now() is obvious, but I could also say that there is an imaginary isNow that relates to isBefore and isAfter.
@grimpirate I see your point about isBefore(Time::now()) being explicit, but I still believe isPast() and isFuture() improve readability and developer experience. IMO these methods are intuitive and reduce repetitive code.
Changing the method signature of isBefore() and isAfter() to accept null and assume Time::now() does not improve readability - it actually makes things more confusing. The method name isBefore() without a parameter is not intuitive - it doesn’t clearly convey that it’s comparing against Time::now(). On the other hand, isPast() and isFuture() are IMO immediately clear in meaning. While extending the class is an option, this can be a small yet useful addition that benefits most developers without adding complexity to the framework. |