Rhymond/go-money

Panic on money.Allocate()

evilnoxx opened this issue · 3 comments

Hello,

The Allocate function can panic in the if the given ratios are all zero.
This makes sense, since the function will try to divide de given amount in parts of zero.

However, in my application it can make sense to try to allocate zero money, which would be the sum of some item prices, using those same item prices as ratios. Since the items add up to zero, because they are free, you get a call like

amount := money.New(0, money.EUR)
parties, err := amount.Allocate(0,0,0)

I wonder if, in this case, the Allocate function shouldn't return a slice filled with zeros with the same length as the given ratios, instead of panicking while dividing by zero.

On the other hand, Allocating is just another word for dividing, and zero divided by zero should be undefined, and not zero.

The software must be prepared to deal with these types of situations instead of giving Panic. Returning a slice filled with zeros with the same length as the given ratios or a graceful error seems like good options

I totally agree with @DianaPinheiro, the code must be deterministic.
I would expect zeroes should be returned on a given example. When you try to divide 0 cents to parties, everyone should get 0. @evilnoxx would you have time to work on the PR?

@Rhymond I gave it a shot. #123