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
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:
Moneyeuros=newMoney(someValue,"EUR");if(euros==Money.MaxValue)// will throw InvalidCurrencyException{// Do something}
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.
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.
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.
The text was updated successfully, but these errors were encountered:
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:
The Case for Allowing Neutral Money in Comparisons and Operations:
Money.MaxValue
andMoney.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.Money.MaxValue
orMoney.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.Money.MaxValue
to anotherMoney
) to succeed without anInvalidCurrencyException
.The Counter-Argument — Throwing Exceptions:
One might argue that allowing comparisons or operations between
Money
objects withCurrency.NoCurrency
and specific currencies could lead to inconsistent behavior or unclear semantics. For example:$100 USD
("USD"
) to a "neutral, deterministic max value" likeMaxValue
withCurrency.NoCurrency
?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.Compromise Solution — Context-Dependent Logic:
NoCurrency
, to participate safely in certain operations, but only in operations where the behavior is clear and consistent:==
,<
,>
, etc.) could treatMoney
withNoCurrency
as neutral, abstract values, so it becomes possible to compareMoney.MaxValue
orMoney.MinValue
to aMoney
with a specific currency without throwing exceptions.Money.MaxValue > $100 USD
could always evaluate totrue
, irrespective of the currency.NoCurrency
could remain invalid to prevent ambiguity, throwing anInvalidCurrencyException
.Draft solution
The last solution sounds the best. Allowing
NoCurrency
(neutral Money) in comparisons while disallowing mathematical operations strikes a reasonable balance. This approach:Money.MaxValue
andMoney.MinValue
deterministic and culture-agnostic, aligning with your design goals.Currency.NoCurrency
don't make clear sense (like addition or multiplication).The text was updated successfully, but these errors were encountered: