Improve null handling in getNextQuotaReset - #1232
Conversation
The original code used 'referenceDate ?? now' which meant when referenceDate was null, it created a new Date from now, then the while loop would immediately add a month since nextMonth <= now would be true (they're equal). This is inefficient and the intent is unclear. Made the null case explicit: when referenceDate is null, return next month from now directly. When referenceDate is provided, start from that date and add months until it's in the future, which is the original behavior.
|
Thanks for taking a look at There's no inefficiency being fixed: the loop only executes a single iteration in the null case, not an unbounded number. It's not doing extra work, and the output is identical for every input. The PR description asserts a bug ("the while loop would immediately add a month") but that's the intended and correct behavior, not a defect — quota reset should always be strictly in the future relative to Splitting into two code paths does add a few lines without adding test coverage, and since the behavior is unchanged there's nothing to validate against anyway. If you want to improve this function, a better target would be adding tests for Closing as this doesn't change observable behavior and the stated bug doesn't exist. |
Overview
Improve null handling in the
getNextQuotaResetfunction incommon/src/util/dates.ts.Bug Description
The original code used
referenceDate ?? nowwhich meant when referenceDate was null, it created a new Date from now, then the while loop would immediately add a month sincenextMonth <= nowwould be true (they're equal).This is inefficient and the intent is unclear.
Fix
Made the null case explicit:
Testing
No existing tests for this function, but the fix improves clarity and efficiency.
Files Changed
common/src/util/dates.ts- Improved null handlingScope
This change only touches
common/which is an approved contribution area per the Contributing Guide.