Skip to content

Latest commit

 

History

History
73 lines (49 loc) · 2.86 KB

02.portfolio.md

File metadata and controls

73 lines (49 loc) · 2.86 KB

Implement Portfolio

From now on, we would like to handle operations between amounts in different currencies:

  • 5 USD + 10 EUR = 17 USD
  • 1 USD + 1100 KRW = 2200 KRW

We need to bring a new business concept to our implementation: a Portfolio.

Basically, a Portfolio contains a list of amounts in various currencies.

Taking our first example above, it could contain 5 USD and 10 EUR.

We should be able to evaluate the full Portfolio amount in a given currency:

  • USD => 17 USD
  • EUR => 14,1 EUR
  • KRW => 18940 KRW
  • etc.

Your mission in this iteration is to implement the Portfolio.

Write a first test: 5 USD + 10 EUR = 17 USD

Don't forget to write your code using TDD.

Small reminder regarding it: the Red step is either a test failure or a compilation error.

Please take some time to read how to generate Code From Usage.

There are different strategies when you want to go from a red state to a green state:

  • Fake it until you make it
  • Obvious implementation
  • Triangulation

We recommend to use Fake it until you make it for this first one.

Write a second test: 1 USD + 1100 KRW = 2200 KRW

Write a new failing test.

Use Fake it until you make it once again.

Triangulation

Now that you have 2 test cases, you can now use Triangulation and refactor the production code to get you closer to the "final" solution. Your 2 test cases will act as a safety net for your refactoring.

What can be improved?

Always think about what could happen at the edge of your system.

  • From your code implementation, what happens if we add in the same Portfolio many amounts of the same currency?
    • We need to increase our confidence by adding a new test on it: 5 USD + 10 EUR + 4 EUR = 21.8 USD for example
  • Regarding the current implementation, we may improve error handling and throws a global exception with the details of all the missing exchange rates
    • Instead of throwing an Exception on the first one missing

Add a new Test Case: 5 USD + 10 EUR + 4 EUR = 21.8 USD

Write this new [failing] test.

Improve error handling

If we have multiple missing exchange rates we return the information only for the first missing one... Let's improve it: write a new test that will drive you to the final implementation.

The test could look like this: 1 EUR + 1 USD + 1 KRW = ? EUR

Reflect

During this iteration we have implemented a Portfolio that allows to add different amounts in different currencies.

Take a look at your test cases, any duplication?

Generate code from usage

Always put the same attention on your test code than on your production code

Step-by-step solution