-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
difference()
should throw if arguments are in the "wrong" order
#663
Comments
Fixes tc39#663. Fixes tc39#597. Changes `difference()` to throw if `other`is larger than `this`. Goal: encourage feedback about long-term solution: option 1 - previous behavior: `difference()` returns an absolute value option 2 - behavior in this commit: throw if other is larger option 3 - negative durations (tc39#558) Tests are updated to make it easy to choose any of the three options. Tests for option 1 are commented out in case we want to revert. The rest of test changes were reversing arguments so that choosing any of the options will break few tests.
Fixes tc39#663. Changes `difference()` to throw if `other`is larger than `this`. Goal is encourage feedback about long-term solution: option 1 - previous behavior: `difference()` returns an absolute value option 2 - behavior in this commit: throw if other is larger option 3 - negative durations (tc39#558) Tests are updated to make it easy to choose any of the three options. Tests for option 1 are commented out in case we want to revert. The rest of test changes were reversing arguments so that choosing any of the options will break few tests. Because `difference` is now unidirectional, it doesn't make sense to restrict `Time.prototype.difference` to 12 hours max. Fixes tc39#597.
Fixes tc39#663. Changes `difference()` to throw if `other`is larger than `this`. Goal is encourage feedback about long-term solution: option 1 - previous behavior: `difference()` returns an absolute value option 2 - behavior in this commit: throw if other is larger option 3 - negative durations (tc39#558) Tests are updated to make it easy to choose any of the three options. Tests for option 1 are commented out in case we want to revert. The rest of test changes were reversing arguments so that choosing any of the options will break few tests. Because `difference` is now unidirectional, it doesn't make sense to restrict `Time.prototype.difference` to 12 hours max. Fixes tc39#597.
Another option for |
Yep. Another way to address this could be to offer a new opt-in option, e.g. But honestly I don't feel strongly about these issues either way. |
Fixes tc39#663. Changes `difference()` methods to throw if `other`is larger than `this`. Goal is encourage feedback about long-term solution: option 1 - previous behavior: `difference()` returns an absolute value option 2 - behavior in this commit: throw if other is larger option 3 - negative durations (tc39#558) Tests are updated to make it easy to choose any of the three options. Only a few tests were functionally changed (e.g. to add throws() tests). The rest of test changes were reversing arguments so that choosing any of the options later will break relatively few tests. Because `difference` is now unidirectional, it doesn't make sense to restrict `Time.prototype.difference` to 12 hours max. Fixes tc39#597.
Fixes tc39#663. Changes `difference()` methods to throw if `other`is larger than `this`. Goal is encourage feedback about long-term solution: option 1 - previous behavior: `difference()` returns an absolute value option 2 - behavior in this commit: throw if other is larger option 3 - negative durations (tc39#558) Tests are updated to make it easy to choose any of the three options. Only a few tests were functionally changed (e.g. to add throws() tests). The rest of test changes were reversing arguments so that choosing any of the options later will break relatively few tests. Because `difference` is now unidirectional, it doesn't make sense to restrict `Time.prototype.difference` to 12 hours max. Fixes tc39#597.
…pe.difference` to 12 hours (#667) * difference: throw out-of-order; zap Time 12h limit Fixes #663. Changes `difference()` methods to throw if `other`is larger than `this`. Goal is encourage feedback about long-term solution: option 1 - previous behavior: `difference()` returns an absolute value option 2 - behavior in this commit: throw if other is larger option 3 - negative durations (#558) Tests are updated to make it easy to choose any of the three options. Only a few tests were functionally changed (e.g. to add throws() tests). The rest of test changes were reversing arguments so that choosing any of the options later will break relatively few tests. Because `difference` is now unidirectional, it doesn't make sense to restrict `Time.prototype.difference` to 12 hours max. Fixes #597.
Currently,
difference()
returns the absolute value of the difference between two Temporal instances. I'm concerned that this behavior will lead to bugs in cases where a developer believes that two values are always going to be in a particular order, while in reality they can sometimes unexpectedly be in the opposite order.Here's a few examples where this can happen:
FWIW, I've worked with teams that have run into all of the above issues in the wild.
Temporal can't stop developers from making ordering assumptions that turn out to be wrong, but Temporal can help developers find these bugs easier by making wrong-order
difference
bugs easier to find.With the current behavior,
difference()
will return a normal-looking result and the bug will likely propagate downstream and will be hard to diagnose, especially if the difference is small.My suggestion is that
difference()
should enforce the order of its arguments by throwing if passed arguments in the "wrong" order.Other benefits:
Time.prototype.difference()
would make it unnecessary to limit results to 12 hours. Max of 12 hours fromTime.prototype.difference
is confusing #597Calendar.prototype.difference
that already requires ordered arguments.If we do enforce an order, I'd suggest the
other
argument must be smaller. This mirrorssubtract
andcompare
where if the larger value is first then a positive result is returned.difference
would work the same, except that negative values are treated as errors. Given that, without negative durations,compare
anddifference
will often need to be called as a pair, having them be in the same order in would be helpful.This issue emerged from the 11 June champions meeting discussion.
The text was updated successfully, but these errors were encountered: