Chapter 8. Evaluating a Portfolio
Money itself isn’t lost or made. It’s simply transferred from one perception to another. Like magic.
Gordon Gekko, Wall Street (the movie)
We’ve dallied around the question of how to convert the several Money
entities in a Portfolio
into a single currency
. Let’s dally no longer!
The next feature on our list is the one dealing with mixed currencies:
5 USD × 2 = 10 USD |
10 EUR × 2 = 20 EUR |
4002 KRW / 4 = 1000.5 KRW |
5 USD + 10 USD = 15 USD |
Separate test code from production code |
Remove redundant tests |
5 USD + 10 EUR = 17 USD |
1 USD + 1100 KRW = 2200 KRW |
Mixing Money
A heterogeneous combination of currencies demands that we create a new abstraction in our code: the conversion of money from one currency to another. This requires establishing some ground rules about currency conversions, drawn from our problem domain:
- Conversion always relates a pair of currencies.
-
This is important because we want all conversions to be independent. It does happen in reality that multiple currencies are “pegged” to one single currency—which means that a particular exchange rate is fixed de jure.1 Even in such cases, it’s important to treat each pegged relationship as a distinct pair.
- Conversion is from one currency to another with a well-defined exchange rate.
-
The exchange rate—the number of units of the “to” currency we get for one unit of the “from” currency—is a key component of currency conversion. The exchange rate is represented by a fractional ...
Get Learning Test-Driven Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.