Linked by MOS6510 on Wed 17th Apr 2013 21:20 UTC
General Development "You often hear about how important it is to write 'readable code'. Developers have pretty strong opinions about what makes code more readable. The more senior the developer, the stronger the opinion. But, have you ever stopped to think about what really makes code readable?"
Permalink for comment 559260
To read all comments associated with this story, please click here.
RE[4]: Comment by lucas_maximus
by Alfman on Fri 19th Apr 2013 14:58 UTC in reply to "RE[3]: Comment by lucas_maximus"
Member since:


"As for dealing with money, I still wouldn't want to type it as Integer or Decimal (and definitely never Float). I'd declare my own Money type or Dollar type or whatever was the most descriptive name appropriate to the task and then use that."

If a language has a decimal type (like databases do), that would be ideal, why wouldn't you want to use that?

In practice though many web languages have such weak typing you don't have much choice (javascript/php). Floats aren't great for cash, but it's not usually a noticeable problem outside of banking because typical cash arithmetic doesn't produce irrational numbers, so rounding should be sufficient. However to be honest I wasn't even sure which way fractional taxes etc *should* be rounded.

If you buy two items for $1 each, and tax is charged at the end, it could result in discrepancies if you return them individually.

subtotal = $1 * 2 = $2.00
tax = $2.00 * 6.25% = $0.125 # round up or down?
total = $2.125 # round up or down?

Obviously if the purchase price is not evenly divisible by the number of products, then there is no way for all the returns to be valued the same if they're to equal 100%. Does anyone know whether the tax on returns typically gets recalculated in the context of the original transaction? If they are done as new transactions, one may be able to exploit the rounding differences to earn a penny ;)

"BTW, this is why I've grown a tad contemptuous of C*/Java type systems and the folk who hold them up as the One True Way Typing Should Be Done. Compared to the sorts of types systems you can find in functional and other 'exotic' languages, C*/Java typing is downright crude in concept and execution, limited in its expressiveness and mindlessly bureaucratic to use."

It's not clear to me what you are criticizing. Both of these languages let you create your own Dollar class if you wanted to.

I do criticize languages for not having unsigned types (java, vb), this occasionally causes problems when the data type is truly not supposed to be signed.

Reply Parent Score: 2