Javadoc-driven development

March 17th, 2006

Its the new development methodology thats taking the developer-world by storm…. ;)

Well, not really.

Just a cute phrase that occurred to me today when chatting to a colleague. We’d just spent 15 minutes discussing the desired behaviour of a system I was working on. Usual stuff – at first blush it seems straightforward, but plenty of edge cases, historical behaviour requirements, and other nastiness.

So we got it figured out, and its 11:45. Lunch booked at 12:00. No way am I even getting 10% of the code done in that time. But its just enough time to do the Javadoc.

So, 15 minutes later, my code has the 2 method signatures I need and around 40 lines of Javadoc describing the code’s behaviour, exceptions, and assertions. And after lunch, I was able to get straight into coding, all details of my discussion safely preserved.

And as an added bonus, when it comes time to rework it, I (or the next guy) will have some great doco to get started.

Not rocket science. Certainly not worthy of its own buzzword-esque title (although it does roll off the tongue :) ). And probably something everybody should be doing anyway as part of their actual methodology. But its a neat little anecdote worth telling!

2006 Honda Civic hybrid vs 2005 Prius

March 16th, 2006

I’m currently the proud owner of a Toyota Prius (2004 model). Had it a couple of years and don’t have a single bad word to say about it. The fuel efficiency is fantastic (typically half, to a third of my co-workers’ non-hybrid vehicles), all the features are there (cruise control, climate control, etc) and the number of compromises I have to make seem to be exactly zero.

Whats not to love? :)

At purchase time, there were few options in the “environmentally friendly” space (is that an oxymoron when it comes to cars?). But a few months later, the Honda Civic hybrid became available. When the specs came out, I was still happy with my decision. The main factor was that the Civic’s fuel economy was noticeably worse (a number of small, non-hybrids do better, from what I can tell).

Well, its a new year, and Honda have brought out a new Honda Civic Hybrid. The styling looks great, they’ve increased the power, and electric-engine capability, and it seems to have more of a nod to fuel efficiency (in particular it can now, like the Prius, run on electric power alone at low speeds).

I came across the following review comparing the new Civic Hybrid to the current Prius, which might be of interest to people looking into Hybrids (or current Prius owners who want to check out the next generation of hybrids!):

http://www.edmunds.com/insideline/do/Drives/Comparos/articleId=108445

Non-US people (like me) should be aware that this is a US review, so some details may vary between what they’re reviewing and what is available to you. Also note that the Prius they use for comparison is 2004, but apparently the 2004 model is virtually identical to 2005.

In short, the review says that the Honda Civic is greatly improved, but that the Prius retains its edge. Its close, and the review goes on to say that the Civic may be better for certain people (especially those doing a lot of highway driving) – so, when it comes time to look at upgrading, I’ll certainly give the Civic a test drive.

In summary, still feel great about my original Prius purchase decision! ;) And, if such things interest you, I’d really recommend checking out the review. Even if you’re not that interested in the upshot of the comparison, it’s educational to see the current state of hybrids, and what can differentiate one from the other.

How did I miss this? java.util.Collections

March 13th, 2006

Well, they say you learn something new everyday. And given the size of the Java API, thats not too hard to believe ;)

After doing serious Java programming for at a least a few years, and having dabbled for a few years more (all the way back to the 1.0 days!), I’m pretty happy with my breadth of Java knowledge. Until a co-worker today mentioned using Collections.shuffle() to randomize a list.

“Pffffft”, says I, “Surely you jest”, says I. “No such thing.”

Well, there is. java.util.Collections (not Collection, note – theres an “s” on the end!). A nice, chunky class, full of juicy, static methods just waiting to save you from having to re-invent the wheel when it comes to operating on Collections. There’s the obvious stuff – shuffle() and sort() to randomize and sort, for instance. And small, but useful stuff – min() and max(), swap(). And some real useful stuff, like synchronizedList() (which returns a thread safe version of your list).

Many’s the time I’ve done my own “max()” routine. Each time, I trawled through the Collection interface looking for one and was mildly surprised it didn’t exist. And I never thought to look any further afield. I’m guessing that there may be one or two Java guys out there who are in the same boat (How many library classes are there now in Java?). Seeing as my co-worker doesn’t have his own blog…yet (come on H! Get your act together! ;) ), I’m doing this posting for the other people like me!

Of course, I’ve always got the excuse for my ignorance that its a relatively recent addition. I mean, its only been around since Java 1.2….. *grin*

Will Wright’s “Spore”

March 2nd, 2006

Came across this link on digg for a 30 minute video demonstration of the upcoming game “Spore”.

Wow!

I gather the game is the brainchild of Will Wright (of “The Sims” fame). The demo blew me away – it is sooooo open-ended. I won’t try to do it justice here – for one thing the scope seems huge, and for another, this was just a demo, so the finished product could be wildly different. In short though, you guide a species’ evolution to a certain stage, then switch to a kind of SimCity/RTS type game to build up their civilisation, then switch to a kind of “God” mode where you can fly to other planets and do it all again.

Reading over that last paragraph, it doesn’t really do justice to what the demo showed. For instance, you seem to have complete flexibility in designing your creatures – the computer figures out how they move/eat/etc based on their body architecture. And the creations of different players are made available (asynchronously) to your own universe – while exploring the universe, you might encounter a planet that has been copied from some other player.

I really recommend setting aside the time to watch the video. Its been over 2 years since I bought a computer game, but I can’t wait for this thing to be released.!

(btw – there’s a good article on the Wikipedia on “Spore” which goes into a little more detail, for those that are interested)

Being too secure with a CGI script

February 27th, 2006

Oooops. I mean, it seemed like such a good idea, right?

While working on the new Asman IT Consulting website a couple of months ago, I wrote a new “Contact Us” CGI script. I often work in the IT Security space, so I always have the security angle high on my list of priorities. So, I did the usual things:

  • I made sure that it wasn’t too flexible. Case in point – While chasing down a spam problem for a client, I discovered they had used a well known “Contact Us” script that was widely available that let you set the “to” address, such as the website administrator, as a hidden field. It worked well, until the spammers realised they could override that in their POSTs and use your Contact Us page as a spam relay…!
  • I sanity checked all fields to make sure only legal characters were present. Not, by the way, the common mistake of checking for illegal characters. Seems so tempting, as there are far fewer illegal characters to check for….. right up until you discover that you overlooked a dangerous character!
  • I checked that the script was only accessed from my “search” web page. Anything else would suggest some automated tool randomly accessing it directly
  • And a host of other checks and practices for things like rate of sending, data size, logging improper use, etc, etc

So far, so good. A few tests showed things working fine. And 2 months later the number of malicious hacking attempts made through it numbered exactly 0.

Exactly as many legitimate “contact us” messages I’ve received, as it happens. ;)

A look through the logs showed the problem. And it was all because of that second-last point.

Turns out I’ve had a few people try to send me messages this month. But a number of the more tech-savvy users (and I’m flattered they’re among those accessing my site!) turn off the “Referrer” facility in their browsers. The “Referrer” field normally allows a site to see where visitors have come from. With this disabled, my “Contact Us” facility refused to process their message as it looked like they had bypassed the appropriate web page within the Asman IT Consulting site.

All fixed now, and my apologies to those who were rejected up till now! But an interesting example of the “Security” vs “Ease of Use” balancing act that comes up time and time again. There was nothing intrinsically wrong with my initial approach. And there is nothing intrinsically wrong with people choosing to configure their browsers in that fashion. Everyone needs to pick a security stance that suits their particular situation.

And, based on their experiences and changing needs, amend that stance as required.

Upgrading to dual flush toilets

February 17th, 2006

As of a week or so ago, my 20 year old townhouse now sports two new dual-flush toilets instead of the original single flush toilets.

This may need explanation in some quarters of the world (if my American friends are to be believed!). In Australia, most toilets are dual flush – one option for “Number Ones” and one for “Number Twos”! ;) The advantage is that you can use a lot less water to flush liquids than solids.

(I should apologise at this point – this post is a little more “earthy” than my usual fare!).

The water savings I achieved are even more dramatic, though, due to improvements in the design of toilets in the last 20 years. Whereas my old toilets used 11 litres per flush, my new toilets use at most 4.5 litres (and only 3 litres for the “number ones” option).

The total cost was $1300. $700 for two new toilets, and $600 for installation.

So, I’ve done a few back-of-the-envelope type calculations and worked out how much water I’ll be saving. Care to guess? Go on, I dare you! :)

16,000 litres a year.

Wow.

Kind of impressive. So I quickly moved on to figure the dramatic saving this would have on my water bill. Turns out this depends on whether I’m a big water user or not, but it works out to be between $8 to $16 a year.

Wow again. But this time, its the other sort of Wow…..

Now, I didn’t go into this to save money – I did it because it was the Right Thing ™. Glad I did it, and I’d urge everyone to do the same. 16,000 litres of water per household is a lot of water. But there’s a lot of people out there who are either unable to afford the cost of upgrading or (to be less generous) to self-focussed to do it unless theres something in it for them.

Full credit at this point to the ACT Government – they have a rebate program that gives you $100 rebate for doing the upgrade – there’s a few rules and conditions, and you can get all the details at http://www.thinkwater.act.gov.au. That amounted to a 15% discount, which is great.

Of course there are cheaper toilets out there (there are also way more expensive ones – do you know there are toilets that cost $1500 each?! How good can using the bathroom be?). And there may be cheaper plumbers. And many people may only want to upgrade a single toilet. Still, householders need to expect to pay at least a couple of hundred dollars.

Wouldn’t it be nice if water was charged at closer to its actual value? Imagine if, for instance, households were allocated some subsistence amount of water for free (or some small fixed sum), then a much higher cost for every kilolitre above that. I’m sure I’m grossly oversimplifying, but I imagine you could come up with some figures that match the average households current water bill. The difference would be that there is suddenly a big incentive to save water.

I’m sure this isn’t a new idea (although I haven’t seen any extensive discussion on it during my travels on the Net). What do you think?

Java coders have it easy – crashing constructors

February 10th, 2006

I came across this entry while scanning Java Blogs yesterday about how to handle exceptions within a constructor. The essence of the article is how do you handle the situation where you couldn’t instantiate your object?

So, if the user passes invalid parameters, do you throw an exception? And, slightly more subtly, if your constructor encounters an exception during object setup, how does it handle this?

Interesting article and well worth the read.

But it struck a bit of a chord with me – I recently bought a Nokia N70 phone, and have been cruising the net looking at how you can code for these things. The N70 is a Symbian phone, and while it has J2ME support, the hardcore stuff seems to be done in C++. Now, C++ is an … interesting language, but is not really known for its in-built protection against memory leaks, and when you’re running a program on a device with limited memory, where your app may well run for the order of days or months this suddenly becomes a big deal.

OK, so you just code, really, really carefully. ;)

Well, turns out that constructors are a special case in terms of memory leaks. Symbian doesn’t use standard C++ exceptions, but it does have an analagous concept called “leaving” grafted on. And not being a C++ guru, I couldn’t tell you what difference there is between normal C++ exceptions and the Symbian variety. In any case, the issue when writing Symbian programs appears to be this:

  1. Constructor called
  2. Constructor initialises an instance variable that points to some new object
  3. On the next line, the constructor encounters an error and leaves

So, we now have allocated memory for some new object (step 2 above), but the object that owns it no longer exists – it crashed during construction. And because the object never fully formed, the destructor wasn’t run on it and so couldn’t free up the memory. Ladies and gentlemen, we have memory leak…. :(

Turns out that Symbian programs have an accepted idiom on how to handle this stuff. Objects that can suffer this way have a static construction method (like the whole getInstance() idiom you see in Java). This static constructor creates the object using a constructor that only performs trivial operations that won’t crash, then once the object exists, calls an instance method on the new object that performs 2nd-phase construction. If things turn to jelly, there is at least a concrete object in existence, so the destructor will be called as normal.

Pretty neat. And pretty eye-opening as to the kind of issues you can be exposed to without garbage collection.

(Which is not to say that Java doesn’t have its own issues with memory leaks, but thats a whole other blog post… :) )

Great site – lifehacker.com

February 3rd, 2006

www.lifehacker.com. Bookmark it. Add it to your aggregator. Print out the URL, frame it, and put in on your bedside table (marginally behind the picture of your “Significant Other” – you don’t want to go too far….)

You get the idea… ;)

I can’t even recall how I found this site now – some random google or another, I think. But I’ve subscribed to the feed for the last few weeks and I’m hooked. What is it? Well, I think they describe it best:

Computers make us more productive. Yeah, right. Lifehacker recommends the downloads, web sites and shortcuts that actually save time. Don’t live to geek; geek to live.

In the last couple of weeks, they’ve posted short reviews and links to cute Firefox plugins and Mac OS X apps, links to stories on hybrid cars, and a number of really interesting links to stories on personal organisation and time management. So far, I’ve been seeing around 10 – 20 posts a day, of which I find at least 75% are worth the read.

Kind of ironic – despite their mission statement, I’ve just lost 5-10 minutes of my life each day to their website! Here’s hoping thats offset by the efficiencies I’ll no doubt gain….. :)

End of rave. We now return you to your regularly scheduled programming…

Thinking about power consumption when buying a TV

February 1st, 2006

Power consumption isn’t the first thing that enters your mind when buying a TV. Picture quality is a big one. How many different inputs it has. And the most importantly – whether its just a bit bigger than the new TV your work mate just bought! ;)

Its worthwhile thinking about power consumption though. I’ve just been trawling a few manufacturers’ sites, trying to get an idea of the differing power consumptions. Obviously, the big difference is between technologies – CRT, LCD, or Plasma. Comparisons are a bit tricky – Plasma TVs are universally at the big end of the size spectrum, followed by LCD, followed by good, old CRTs. In my quick google around the Net, I couldn’t find a size that was available in all technologies, but for the purposes of comparison I’ve looked at 32″ (which you can get in both LCD and CRT), and 42″ for the plasma.

From a quick sample, you’re looking at the following sort of power consumption:

  • 32″ CRT – 220 Watts
  • 32″ LCD – 170 Watts
  • 42″ Plasma – 370 Watts

To put this into a sample scenario, lets say you watch television 6 nights a week. A lot of people turn on the box around 6:30pm, and watch till around 10:30pm. But, you do take that trip to the coast each year for a fortnight. So, over a year, you have the TV on for 50 weeks x 6 days x 4 hours = 1200 hours. (Wow! Thats 50 full days, or 150 8 hour working days. We have to start getting out more!)

What does that mean in terms of power consumption? Over the year, we’re looking at the following:

  • CRT: 264 KwH/year
  • LCD: 204 KwH/year
  • Plasma: 444KwH/year

Here in Canberra we currently pay 9.9c/KwH for our electricity. So, it costs around $20/year for the CRT, $26/year for the LCD, and $44/year for the plasma. Obviously electricity prices can vary widely from area to area – this just gives an idea.

Aside from cost, though, there’s the environmental issue to consider. Depending on your electricity provider, there’s typically 1.44 Kg CO2 produced for every KwH, so in the case of the Plasma TV, that’s over half a tonne of CO2 per year… :(

Just one more thing to think about, as if TV buying wasn’t complicated enough…

Yet another Eclipse / NetBeans comparison

January 29th, 2006

A week or so ago, I downloaded NetBeans 5 RC2 specifically to play with the mobility pack, but figured I might as well explore its capabilities as a J2SE IDE.

For those of you who like their blog posts short and snappy – “Neat, some cool stuff in there, but I’ll be sticking with Eclipse for now”. For those who can handle a couple more paragraphs, read on…. :)

To start with, can I join the crowd and say “Matisse rocks!”? Matisse is the visual editor for Swing apps, and it is fantastic – in 90% of cases it does what you mean: you put an object right on the left border, and it figures that that object should be anchored to the left. You place one button next to another and it keeps them together and snaps them to be level. I played with Eclipse’s Visual Editor in a recent small project, and it has a ways to go to catch up to this.

But (and there’s always a “but”, right?), the killer for me was that “90% of cases” phrase – its the other 10% that comes back to bite you. For instance, I wanted to do a a little “cross” of buttons – and up, down, left and right, laid out in a diamond. After having been spoilt with the editor doing what I meant for the rest of the UI, I found it a bit jarring when I was doing “free form” component layout – buttons were slightly off, by a pixel or two and it kind of “looked” wrong.

There’s probably a hidden “layout in a cross” option I missed :) . And I could have added a panel, set it to GridBagLayout and done it that way (Matisse has good support for GridBag). And how often do you want to have buttons in a bizarre shape like that anyway? But it kind of broke the spell for me in a way. I’m a big believer of being “in the zone”, development-wise, and switching between IDE modes (code and visual) is a real context switch for your brain – to compensate, I think I need almost perfection from the Visual Editor. Matisse is soooooo close, but isn’t quite there for the fiddly cases. (How would I have liked Matisse to handle my peculiar layout requirement? I have no idea!).

Matisse is the big ticket item – the rest of the IDE is good, but not compelling from where I sit. There are some winners, though. I love the idea that the IDE is ant based – take your development directory, zip it up and give it to someone else, and they can compile, test, build, etc. Ant seems to be the one big constant across teams – it makes so much sense to have it as the backbone of your development processes. And the fact that a test folder is created when you start a new project is a little nicety, but a good one.

On the downside, on my machine (2 Ghz with a ridiculous 512Mb of RAM), NetBeans is noticeably more sluggish than Eclipse – maybe this is an SWT thing? (I hope not, I’m one of those Swing fans ;) ). And the plugin environment for Eclipse seems so much richer at the moment – I even have a VI plugin for my Eclipse for when I’ve been doing a lot of Unix work and keep on putting the letter “i” throughout my source code (you Unix guys will know what I mean)!

So, I’m staying with my Eclipse for now. It was certainly worthwhile to check out though, and I’m still planning to do my next little J2ME project with NetBeans – I’m keen to give that Mobility Pack a proper workout.