the corner office

a blog, by Colin Pretorius

# Order matters

I ran into an odd problem tonight. I have a simple Money class which wraps a BigDecimal and provides a safe subset of BigDecimal's methods while enforcing a sensible scale.

Any guess what could be wrong with the following code snippet?

public class Money {

    public static final Money ZERO = new Money(0); 
	
    private static final MathContext MC = MathContext.UNLIMITED;
	
    private final BigDecimal amt;
	
    public Money(double amt) {
        this.amt = new BigDecimal(amt, MC);
    }

    ...

If it's obvious to you, give yourself a Noddy Badge. The BigDecimal constructor gets given the parameter and the MC constant, but throws a NullPointerException, because MC is null. Why? Because ZERO is constructed before MC. It would be nice to think that all static code is executed before anything else, but it makes sense that you couldn't have that, in this case.

It would also be nice to think that the Java compiler would reorder static code to make it work, but that isn't too practical either. Static code is executed sequentially, and so ZERO is constructed before MC.

I like keeping variables ordered public-first, then private, but in this case I'd have to swap the two statements around. An alternative is to use a static block, a la:

public class Money {

    public static final Money ZERO; 
	
    private static final MathContext MC;

    static {
        MC = MathContext.UNLIMITED;    
        ZERO = new Money(0);
    }

    private final BigDecimal amt;
	
    public Money(BigDecimal amt) {
        this.amt = new BigDecimal(amt.doubleValue(), MC);
    }

    ...

This is probably better regardless, since it makes the order of instantiation more explicit. What's more, you could put the static block before the declarations, and it'll still work.

There are people who've read the Java language spec and could quote chapter and verse and wouldn't make mistakes like this. I'm not one of them :-)

(Update: as an added bonus, the first version of this post wasn't using MathContext properly, which I've only just discovered. That's probably worth a post in its own right. Silly BigDecimals.)

File under: java : {2007.11.26 - 21:26} : Comments (0)

# Ramble

Right, so, a techie ramble is long overdue. As I mentioned, I'm playing with some new stuff at work. My main new thrill is Spring. All I ever knew about Spring before was that it (a) relied heavily on Dependency Injection, (b) was decried for being XML intensive, and (c) featured in a very large percentage of job adverts.

I like it. I like the DI approach to things - not necessarily because it's the only virtuous way of putting together large systems, but because it makes life easier in all sorts of ways. If you've ever battled with rigging up mock objects for a test environment, you know that it's a helluva lot cleaner than the alternatives.

Spring has had a few other nice surprises for me. It's a gentle introduction to pain-free AOP. I've always considered AOP to be intellectually interesting but not very practical for a lazy lay developer - in terms of esoteric tooling, and limited adoption. Spring is powerful and entrenched enough that it solves both those problems. Spring's JDBC support is also great. I've tried my best to like Hibernate, and I know it's incredibly powerful if you're willing to submit to the Hibernate way, but I'm still happiest cranking out SQL statements and knowing exactly when I'm hitting the database and why. Spring takes out a lot of the drudgery and housekeeping, which is a real time-saver.

On the home front, I'm still plodding on with Eclipse RCP development, mainly adding bits and pieces to our home budgeting app. I spent a bit of time moving parts of it over to Swing using Matisse4MyEclipse, but in the end I gave up. I liked Matisse4MyEclipse and I'd gladly recommend it for working with Swing - but I reached the point where I had to admit to myself that no matter how convenient and powerful the tooling, I just don't like Swing. It's mainly an aesthetic thing, I'll admit, and I wouldn't call myself an SWT/JFace fan, but the only Java app I spend a significant amount of time using is Eclipse, so go figure.

File under: techie : {2007.11.25 - 14:50} : Comments (0)

# Public transport

I remember my first impressions of London commuters at Paddington and on the tube. Sour, in a rush, unforgiving and impatient with anyone walking too slowly or getting in the way or not doing the right thing. It's only been two weeks, but already I find myself becoming like them. More to the point, I understand, now. Places to go, trains and tubes to catch, and eager to get out of the mass of people as quickly as you can. So many people, everywhere, all the time. You switch everybody off. Very different to the bus commute into Oxford, where most people were regulars, I saw them every day, and everything was altogether more relaxed.

It's not just a city slicker thing, either. I lived in Joburg for years. It's a big city, but apart from shopping malls and the like, middle class peopled were never really bunched up. You always had space. Even in long, drawn-out traffic jams, you'd be in a car, with nobody close to you. London transport is completely the opposite.

I'm adjusting to the commuting, though. It takes a big chunk out of my day, but on the upside I'm reading even more - I'm averaging about two novels a week. And the exercise is good, too. In addition to more walking, there's all the standing on the tube, and on the train, because First Not-so-great Western can't be arsed to increase their capacity while fleecing their customers. Bastards. The directors of FGW ought to be lined up against a wall and shot. Or better yet, get them to draw straws and march the losing half off to the wall, and then tell the remaining half that they've got a month to sort their shit out or they're next. I'll bet you that would get results.

File under: personal : {2007.11.16 - 22:50} : Comments (0)

# Older, uglier and supposedly wiser

I turned 35 this week. I never thought about it much until the day arrived, but 35 is a bit of a milestone age. You're no longer an 'early' thirty-something, you're now officially mid-30s, and more accurately, you're on a one-way trip to the arse-end of your thirties, and there's no getting around it.

Perhaps most ominous, is the knowledge that the next milestone is 40, and that carries enough baggage. You're supposed to start thinking about mid-life crises when you hit 40. I always joke that I had my mid-life crisis in my 20s, and I'm only barely getting out of that. So I'm not quite sure where that leaves me. One of these days I'm going to have to start acting my age. Ronwen say I'm a grumpy old man, but I'm still not fully reconciled with the idea of being a grown-up.

File under: personal : {2007.11.11 - 22:36} : Comments (0)

# Into the smoke

Oxfordshire is a beautiful part of world, and working in Oxford has been really cool. I learned a lot of new things, got to work with some great people, and got to spend some time smack bang in the middle of one of the oldest and most beautiful cities in the world. London's call was just too strong though; as lowly immigrants we couldn't afford to ignore what London offers in terms of salary and career development.

So what it boils down to, is that I started a new job in London on Thursday. I'll continue the tradition and say bugger-all about it here, other than that I'll be getting my teeth into and may blog about a few new (to me) technologies. We'll start looking for a place in London, and plan to move early in the new year, but for now, I'm commuting and doing the train & tube thing for the first time. That should be fodder for a few choice grumbles, I suspect.

All in all though, it's an exciting step. Daunting, but exciting. London is something else, and it's always felt a million miles away from where we live out in the country. Now, this insanely huge and bustling city is part of my daily routine. Hard to believe.

File under: personal : {2007.11.04 - 00:05} : Comments (2)

meta

-home-
about
contact
disclaimer
articles
code
tech blog

style: [?]
[plain.dark.blue]

Categories

java
linux
music
notes/domino
personal
politiek
studies
techie
thee_blog
world

RSS Feeds

rssfeed all posts
rssfeed all cmts
rssfeed tech posts
rssfeed tech cmts

Archives

2010.09
2010.08
2010.07
2010.06
2010.05
2010.04
2010.03
2010.02
2010.01
2009.12
2009.11
2009.10
2009.09
2009.08
2009.07
2009.06
2009.05
2009.04
2009.03
2009.02
2009.01
2008.12
2008.11
2008.10
2008.09
2008.08
2008.07
2008.06
2008.05
2008.04
2008.03
2008.02
2008.01
2007.12
2007.11
2007.10
2007.09
2007.08
2007.07
2007.06
2007.05
2007.04
2007.03
2007.02
2007.01
2006.12
2006.11
2006.10
2006.09
2006.08
2006.07
2006.06
2006.05
2006.04
2006.03
2006.02
2006.01
2005.12
2005.11
2005.10
2005.09
2005.08
2005.07
2005.06
2005.05
2005.04
2005.03
2005.02
2005.01
2004.12
2004.11
2004.10
2004.09
2004.08
2004.07
2004.06
2004.05
2004.04
2004.03
2004.02
2004.01
2003.12
2003.11
2003.10
2003.09
2003.08
2003.07
2003.06

© Colin Pretorius