'Seven Ways' OnJava article
Friday, April 30th, 2004Some interesting things in Robert Simmons’ article at onjava.com [http://www.onjava.com/pub/a/onjava/2004/04/28/hardcorejava.html].
Number 1 says to use a stronger compiler. Possibly a reason to look at the new jikes I guess, but as I’d have to buy new computers to handle the heaviness of IDEs, or accept coding at a slower speed, nothing I’m likely to move to. When I do use Eclipse, I turn on a lot of the warnings and like them.
Code reformating is an interesting one. I’m firmly in the ‘it messes up CVS, so stop sodding reformating the code when you commit your changes’. However, I’m happy to go with it if it’s hooked to the commit. Need to look into this at some point. Figure it out in CVS and SVN and I assume use Jalopy.
Introduce final everywhere. Robert Simmons seems to think this is an easy win, but I’m utterly not convinced. It’s not transparant to the developer [as checkstyle would be] and would become a crutch [developers would not expect to have such problems when they forget to put final in, we would need checkstyle to start complaining if final was missing].
Incidentally, I hate the for(Iterator itr = collections.iterator….) style that Robert’s using in his example. It’s amazingly ugly and evil, but sadly has 1 advantage, that the ‘itr’ variable is in a tighter scope. This can be solved in the while loop way with an extra set of braces, but it is equally ugly.
Removing commented out code is good, though I think it should be done by the person who is responsible for a project. This is possibly an oddity of mine; I believe in code responsibility. Ownership with an open door. I often delete old comments etc, but I also comment things out if the business needs imply that that functionalityy will need to go in someday.
Removing anonymous classes is wrong. They’re the closest thing we have to closures. I suspect that Robert has seen them overused a lot. I generally only ever use them for Listeners in GUI work where they seem to fit well. Occasionally a Comparator, but I hate writing non-generic comparators. Robert also has it wrong when he suggests copy and pasting the anonymous class. You should make it more generic, rather than copy and paste some hacked out anonymous piece.
Robert’s 6th tip is mis-titled. Took me the second time to notice what it was as the title made me think it was a different Listener pattern. Instead it just suggests that the collection for Listener support should use Weak references. Well, this is not something the developer does often, it’s something Sun and whatever Framework is being used should listen to.
Lastly we have something about constants. No real argument here. I tend to do this kind of thing with the loose and easy:
public final static Object RED = "RED";
ie) I worry more about the ‘int’, than the fact that someone might put an object in which isn’t a ‘Colour’.
So upshot of the article? I might investigate Jalopy and Checkstyle again, get Jalopy integrated into my SVN and CVS modules and Checkstyle into my ‘maven site’, and will add it somehow to the nightly-build report at work.
