Skip to content
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

Allow Operations with Money that has NoCurrency #99

Open
RemyDuijkeren opened this issue Jan 18, 2025 · 1 comment
Open

Allow Operations with Money that has NoCurrency #99

RemyDuijkeren opened this issue Jan 18, 2025 · 1 comment
Assignees
Labels
Milestone

Comments

@RemyDuijkeren
Copy link
Owner

RemyDuijkeren commented Jan 18, 2025

Some values, like MinValue and MaxValue, return Money where the Currency = NoCurrency (XXX), to represent a deterministic, culture-agnostic, and neutral Currency.

To do operations with Money with neutral Money, it could be beneficial to not throw an InvalidCurrencyException.

For example:

Money euros = new Money(someValue, "EUR");
if (euros == Money.MaxValue) // will throw InvalidCurrencyException
{
    // Do something
}
 
  1. The Case for Allowing Neutral Money in Comparisons and Operations:

    • Having neutral values like Money.MaxValue and Money.MinValue behave deterministically in operations (e.g., comparisons or mathematical operations like additions/subtractions) without throwing exceptions would make the API easier and safer to use, while preserving the idea of neutral values as non-currency-specific constants.
    • These neutral values act as boundary or abstract representations, not tied to real currency. For example:
      • When comparing against Money.MaxValue or Money.MinValue, it’s reasonable to expect that such comparisons should work without failing due to mismatched currencies. They are abstract, culture-agnostic constants, so their comparison value should be derived independently of the currency.
      • Similarly, you might want mathematical operations (like adding Money.MaxValue to another Money) to succeed without an InvalidCurrencyException.
  2. The Counter-Argument — Throwing Exceptions:

    • One might argue that allowing comparisons or operations between Money objects with Currency.NoCurrency and specific currencies could lead to inconsistent behavior or unclear semantics. For example:

      • What does it mean to compare $100 USD ("USD") to a "neutral, deterministic max value" like MaxValue with Currency.NoCurrency?
      • Does adding Money.MaxValue (Currency.NoCurrency) to $100 USD produce a meaningful result or an ambiguous one?
    • By throwing an exception, you ensure that all operations between Money objects are semantic and meaningful only if the currencies explicitly align, thereby ensuring correctness.

  3. Compromise Solution — Context-Dependent Logic:

    • A possible middle ground is to allow neutral currency values, like NoCurrency, to participate safely in certain operations, but only in operations where the behavior is clear and consistent:
      • Comparisons (==, <, >, etc.) could treat Money with NoCurrency as neutral, abstract values, so it becomes possible to compare Money.MaxValue or Money.MinValue to a Money with a specific currency without throwing exceptions.
      • For example, Money.MaxValue > $100 USD could always evaluate to true, irrespective of the currency.
      • However, mathematical operations (like addition or subtraction) involving NoCurrency could remain invalid to prevent ambiguity, throwing an InvalidCurrencyException.

Draft solution

The last solution sounds the best. Allowing NoCurrency (neutral Money) in comparisons while disallowing mathematical operations strikes a reasonable balance. This approach:

  • Keeps boundary constants like Money.MaxValue and Money.MinValue deterministic and culture-agnostic, aligning with your design goals.
  • Prevents ambiguity in scenarios where operations involving Currency.NoCurrency don't make clear sense (like addition or multiplication).
  • Avoids confusion for API users by enforcing consistency across the Money type.
@RemyDuijkeren
Copy link
Owner Author

Add for Adition and Subtraction operations 4692336

@RemyDuijkeren RemyDuijkeren added this to the v2.0 milestone Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant