Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld Talkback >> 958783

Pages: 1
Anonymeuros
Unregistered




Money should be immutable...
      #5164 - 01/03/04 08:01 PM

Interesting article...

I see in your money example, that you use a mutable Money class. Is this wise?

Code:

Money total = ...;
Money amount = ...;

total.increaseBy( amount );



I would certainly recommend that any implementation of a money concept is immutable. Or else how about a changeCurrency method? (rounding errors) or a divideBy method (again rounding errors)...

So instead I would strive for something like this instead

Code:

Money total = ...;
Money amount = ...;

total = total.add( amount );



And of cource support of multi-currencies, so that if total is 5 USD and amount is 1 EUR, then the total after addition should be [5 USD and 1 EUR], not something like 10 USD (ups, sorry - I'm ahead of time )

Just my 2 cents (EUR, so that should a dime to you - ups, sorry - I'm ahead of time again )


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: Money should be immutable... [Re: Anonymeuros]
      #5175 - 01/05/04 04:29 AM

About this "immutable".
Theoreticly nice, yes.
But how do you implemement it?
If you put in evrything as parameters in the constructor
it works fine if you have 1 to 3 attributes.
If you have more attributes your contructor turnes realy ugly.
Have you realy got any benifitts in real life of immutable
classes, i have not.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymeuros
Unregistered




Re: Money should be immutable... [Re: Anonymous]
      #5177 - 01/05/04 05:21 AM

Quote:

About this "immutable".
Theoreticly nice, yes.




Indeed yes!

Quote:


But how do you implemement it?




Done that with exactly this... Money... Use it as much as I can , as it makes life so much easier. :-)
Think about testing for equality... and especially about testing for equality after serialization/deserialization...

Quote:


If you put in evrything as parameters in the constructor
it works fine if you have 1 to 3 attributes.
If you have more attributes your contructor turnes realy ugly.




Agree, but with the example... Money... I can actually just come up with two that make sense to me... currency and amount.

Quote:


Have you realy got any benifitts in real life of immutable
classes, i have not.




Well, I do... and so do you... you just don't know it yet! ;-)


Post Extras: Print Post   Remind Me!   Notify Moderator  
Alexander
stranger


Reged: 09/05/03
Posts: 7
Re: Money should be immutable... [Re: Anonymeuros]
      #5186 - 01/05/04 11:58 AM

Whether one uses an immutable Money class or not is really a question of design. However, I would argue for a mutable one since, in my eyes at least, a Money class represents "an amount of money", which can grow or shrink over time. In this case, the methods "add(Money)" and "remove(Money) throws InsufficientFundsException" make sense.

Again, it's a question of design - there's no FINAL answer (i.e. Money need not be immutable)


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymeuros
Unregistered




Re: Money should be immutable... [Re: Alexander]
      #5209 - 01/06/04 06:18 AM

Quote:

Whether one uses an immutable Money class or not is really a question of design. However, I would argue for a mutable one since, in my eyes at least, a Money class represents "an amount of money", which can grow or shrink over time. In this case, the methods "add(Money)" and "remove(Money) throws InsufficientFundsException" make sense.

Again, it's a question of design - there's no FINAL answer (i.e. Money need not be immutable)




Oh, my God?!?

So you woud like to have something like:

Code:

Money oneBuck = new Money("USD", 1);
Money fiveBucks = new Money("USD", 5);

oneBuck.remove(fiveBucks);



which would throw an InsufficientFundsException ?!?

Great design! Now I'll go and outsource you to India... ;-)



Post Extras: Print Post   Remind Me!   Notify Moderator  
Alexander
stranger


Reged: 09/05/03
Posts: 7
Re: Money should be immutable... [Re: Anonymeuros]
      #5215 - 01/06/04 08:34 AM

A rather vague argument you are putting forward there, Anonymeuros. I would appreciate a more logical clarification than, "Ha ha! I am smarter than you".

To reiterate what I said previously - there are many ways to skin a cat. In the case of a Money class, one *can* use an immutable or mutable one; dependent on what the problem domain dictates. Hence, the example given by Allen Holub.

*Usually* a mutable Money class makes more sense to me, but that's me. That's how I use it. That doesn't mean it's *right* in every application. It depends...


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymeuros
Unregistered




Re: Money should be immutable... [Re: Alexander]
      #5218 - 01/06/04 11:56 AM

Ok, sorry... I couldn't help myself... ;-)

Quote:

...in my eyes at least, a Money class represents "an amount of money", which can grow or shrink over time...




In this case, where you see this Money class as a container holding some amount of money, which can grow or shrink over time... Then of course, it has to be mutable!!! (as it holds an amount that can grow and shrink)

But, then I would argue that you probably could use a better name for your Money class... MoneyBag? Account?!?

Regards,
-a


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: Money should be immutable... [Re: Anonymeuros]
      #5246 - 01/07/04 11:33 AM

One good reason for immutable objects is thread synchronization, it isn't necessary with immutable objects.

Not to say that this reason is sufficient enough!


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: Money should be immutable... [Re: Anonymous]
      #5313 - 01/12/04 05:35 PM

I've would opt for immutable Money. This makes it consitent with other amount objects like Long, Integer, and Double. Making money immutable actually helps prevent obscure bugs and makes code more readable.
Code:

MyAccount account = .. getAccount
AccountView view =
new AccountView(account.getName(), account.getCredits().subtract(account.getDebits());



Do you see the bug?
This immutable code reads bettter and doesn't create a bug.
Code:

MyAccount account = .. getAccount
Money balance = account.getCredits().subtract(account.getDebits());
AccountView view =
new AccountView(account.getName(),balance);




Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: Money should be immutable... [Re: Anonymous]
      #5325 - 01/13/04 12:55 PM

You can translate the mutable/immutable into a real world example.

First, you can think 'immutable' money as a paper money - like Mr. Benjamine $20.

the'mutable' money is "gift certificate card", like Starbuck's card. You can put more money into the card.

The problem is when you are passing 'money' around.

For example, if I give you a green-paper mr. benjamin $20 to you. You can do whatever you want to do with it. I don't have to worry about it and you don't have to worry about it because it is yours.

But if I give you the gift card, I can increase your card amount by upgrading your gift card. Now, you have no clue how much money you have.

With immutable object, you don't have to worry about any other people changing the state.

That's why you have 'String' and 'StringBuffer'.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 1 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 11310

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5