Quote:
Originally Posted by DaWei
If it makes me a curmudgeon to point out when you're being silly, yes, indeed. The OP distinctly asked for a short way. Merely hiding things so that they aren't visible at the point of invocation doesn't qualify as either short or elegant.
|
I disagree. Hiding code away within black box is exactly what object orientation is all about. It's unlikely this solution will affect the program significantly in terms of efficiency, and from the outside it's very concise.
That said, I think the solution is silly, but for reasons different to your own, DaWei. It seems to me as if this:
discount = Percent(20)
origprice = 35.00
saleprice = origprice - discount
Is less understandable than this:
origprice = 35.00
saleprice *= 0.8
Because you're overriding the subtraction operator to do something that isn't immediately obvious. You'd have to keep track of all your Percent objects, or not assign them to any references, otherwise things could get confusing.
Multiplication is well understood by any programmer, and multiplying by 0.8 should instantly be recognisable as a reduction of 20%. Wrapping this up in a class just makes it harder to figure out what's going on.