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?"
Thread beginning with comment 559212
To view parent comment, click here.
To read all comments associated with this story, please click here.
RE[2]: Comment by lucas_maximus
by Soulbender on Fri 19th Apr 2013 05:00 UTC in reply to "RE: Comment by lucas_maximus"
Soulbender
Member since:
2005-08-18

since you can't hold negative coinage in your pocket


No, but you could be in debt.

Yes, that are things to watch out for with number in programming but none if has any relevance to the dubious claim that NumberOfCoins "hides" anything about the nature of the data it stores.

Reply Parent Score: 2

RE[3]: Comment by lucas_maximus
by hhas on Fri 19th Apr 2013 12:22 in reply to "RE[2]: Comment by lucas_maximus"
hhas Member since:
2006-11-28

"since you can't hold negative coinage in your pocket


No, but you could be in debt.
"

Do not mistake Coins, which are shiny pieces of metal of various shapes and sizes, with Money, which is an abstract concept, frequently overdrawn.

Oh sure, if one really insists one can always write stuff like:

int NumberOfCoins = 5;
int DaylightRobbery = -5;
NumberOfCoins = NumberOfCoins + DaylightRobbery;

But then I wouldn't blame either the language nor the reader, but the smarmy smartass that wrote it - because that's exactly the sort of rotten communication skills that rightly give practitioners a bad name.

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. That clearly describes my intent in creating that variable and properly decouples my own program logic from underlying hardware/language implementation.


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.

A good type system ought to be a positive productivity tool for expressing your program's intent as effectively and efficiently as possible. But too often C*/Java typing is used as a personal crutch for lazy thinking, sloppy coding, premature 'optimization' and OCD tendencies: negative programming habits blessed with official seal of approval. Such a mismatch/shortfall is not only a technical deficiency but a cultural one as well. My earlier (long) post has more discussion of why cultural problems are every bit as important as - and fundamentally inseparable from - the technical ones, so I'll not repeat it here.

Reply Parent Score: 2

Alfman Member since:
2011-01-28

hhas,

"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.

http://boston.cbslocal.com/2010/10/01/curious-if-6-25-sales-tax-wil...


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.

Purchase:
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.

http://javamex.com/java_equivalents/unsigned_arithmetic.shtml

Reply Parent Score: 2