Java can be quite silly
So, I’m writing this constructor here:
public Square(double x, double y, double size, double gap) {
…
}
Then I think, really, these should be BigDecimals, because I’m modeling a rectangular area on a grid, which could be of any precision and being maths stuff has to be precise, so I change it to:
public Square(BigDecimal x, BigDecimal y, BigDecimal size, BigDecimal gap) {
…
}
Then I write a test, and I go:
Square square = new Square(new BigDecimal(“1″), new BigDecimal(“1″), new BigDecimal(“0.1″), new BigDecimal(“0.003″));
Note, these could be doubles instead of Strings, but then we’re back to square one – if you want your BigDecimal to be precise it has to be made with a String literal, not double – once you’ve done a double literal, you’ve lost it. Then I think, wait, I’ll have to do this again in a few minutes, that will be an awful lot of “new BigDecimal” there, so I go and add another constructor:
public Square(String x, String y, String size, String gap) {
…
}
Right, now I can use it like this:
Square square = new Square(“1″, “1″, “0.1″, “0.003″);
Great! Except it kind of isn’t… Why should I write numbers as Strings?! What happened to the type safety and all that? I want my Scala toy back
Filed under: programming | Leave a Comment
Tags: BigDecimal, java, programming
No Responses Yet to “Java can be quite silly”