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

Operations not working #4

Open
bitbreakr opened this issue Dec 9, 2020 · 3 comments
Open

Operations not working #4

bitbreakr opened this issue Dec 9, 2020 · 3 comments
Assignees

Comments

@bitbreakr
Copy link

bitbreakr commented Dec 9, 2020

Hello,

Looks like this package does not work when respecting the doc examples

Not working code:

 const fiveEur = new Money(500, Currencies.EUR); // 5 EUR
 // add
 fiveEur.add(new Money(250, Currencies.EUR)); // 7.50 EUR

console.log(fiveEur);
Money { amount: 500, currency: 'EUR' }

Looks like 250 was not added

Working code:

 const fiveEur = new Money(500, Currencies.EUR).add(new Money(250, Currencies.EUR));

console.log(fiveEur);
Money { amount: 7500, currency: 'EUR' }
@macor161
Copy link
Owner

Hi @bitbreakr, thanks for creating this issue. Functions on Money objects always return a new Money instance. This is why the third line of the example (fiveEur.subtract(new Money(470, Currencies.EUR));) returns 0.30 EUR (5.00 - 4.70) and not 2.80 EUR (7.50 - 4.70). Though I agree it could perhaps be a bit clearer. I will add some clarifications in the example this weekend.

@macor161 macor161 self-assigned this Dec 12, 2020
@49e94b8f256530dc0d41f740dfe8a4c1
Copy link

49e94b8f256530dc0d41f740dfe8a4c1 commented Jan 3, 2021

@macor161
Any specific reason you went with immutable objects? Is there a disadvantage to allowing the modification of Money objects?

@virtuoushub
Copy link

@49e94b8f256530dc0d41f740dfe8a4c1, I can not speak to the authors reasoning; however here are some thoughts about immutable and mutable objects, and why a library might use immutable objects (especially one dealing with money where getting the value wrong can be very costly).

From my research, immutable objects

are especially applicable for value types, where objects don't have an identity so they can be easily replaced. And they can make concurrent programming way safer and cleaner (most of the notoriously hard to find concurrency bugs are ultimately caused by mutable state shared between threads).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants