magpiebrain

Sam Newman's site, a Consultant at ThoughtWorks

Posts by samnewman

With “IDEA 4.5”:http://www.jetbrains.com/idea/index.html due out any day now (no doubt Javablogs will be bombarded with product announcements when it happens) I had a look at the forthcoming changes. Whilst a couple of things looked quite handy (such as structural search and replace or improved code duplication checks) there are a few things I’d like to see added:

* If I create a new class prefixed with @Abstract@, I’d like the class created abstract too – this could of course be tailored to individual coding guidelines, so all those Visual Basic refuges prefixing abstract classes with @A@ will be catered for
* If I create a class with an @Impl@ suffix, like @BobImpl@, and IDEA knows of an Interface called @Bob@, it will automatically create the class as a @Bob@ implementation.
* If a class has a test, e.g. @BobImpl@ has @BobImplTest@, then IDEA will provide a link from @BobImpl@ to @BobImplTest@ and vice-versa. A link similar to those provided to show implemented in/implementing method links would be great.
* Given that IDEA understands more links between classes/interfaces, e.g. @Bob@, @AbstractBob@, @BobImpl@, @BobImplTest@, that the refactor->rename would more consistently renamed ‘linked’ classes
* It would be fantastic if IDEA picks up normal code edits as being refactor operations – for example if I change the name of a class by editing, IDEA pops up a dialog asking if I want to perform a refactor->rename.
* A spell checker!
* A Thesaurus to help me with naming…

Thanks have to go out to Simon, who yet again as “uncovered a gem(Fail Fast)”:http://www.brunningonline.net/simon/blog/archives/001460.html of a post over at his blog. Mike Mason’s “Null is bad, hmm-kay?”:http://mikemason.ca/2004/07/03/#040NullIsBad makes the point that when creating new method skeletons (for example when auto-generating methods from tests) the default IDE behaviour is typically to do nothing (in the case of void returns) or return null. Even worse, when auto-generating a method with an array return type IDEA will return the following code:


public Object[] getSomeArray() {
  return new Object[0];
}

This can often result in problems tracking down why a test is failing (or even why normal code is failing) as the unimplemented method is being called, a null is being returned and passed on, and when an error does occur it can often happen quite some way from the original method call.

A better approach of course is to throw an exception from the auto-generated method body detailing why you shouldn’t be calling it. This is a simple matter in IDEA and I’m sure the other IDE’s out there. Flush with my new knowledge I mentioned this to a colleague by way of a helpful piece of advice, only to find he already did it. There is a lesson to be learnt there I’m sure…

The subject of testing abstract classes came up recently. Imagine the following scenario – I have a class @AbstractBob@ which provides an implementation of a method @callFred@. @ConcreteBob@ extends @AbstractBob@ and implements the required methods – at this stage it doesn’t override @callFred@. Now what would you test? There are several approaches I can think of:

# Just test the concrete class
# Test the methods defined in the abstract class by creating a stub implementation, then test the methods explicitly implemented in the concrete class but not those implemented in the abstract.
# Create an abstract @TestCase@ (or whatever – depends on your testing tool) which tests those methods implemented in the abstract class. Then, subclass for testing your concrete class
Continue reading…

No matter whether you call it Test Driven Development or Test Driven Design, many people now accept that the use of the word test is at best misleading and at worst can result in fundamental misunderstanding of what TDD is about.

TDD at its core is the process of defining expected behaviour of your code,make your code match the expected behaviour, refactor, and so the cycle goes. The problem is that presently the only way we have to define the expected behaviour is using test tools such as JUnit. I’ve lost count of the number of people who have become confused over TDD, and have ended up thinking that because their code was developed using TDD that its fully (unit) tested – its not, you just wrote enough tests (specifications) to drive out the behaviour of the system.

We need a new name – call it Behaviour Driven Design, or Specification Driven Design or whatever you want (I’m sure I’m not the first person to use either terms). We need new tools – as long as we use unit testing tools to specify behaviour people will still get confused about what they are actually doing. Even a JUnit clone with different names would go a long way to fixing the misunderstanding. I am of course being slightly disingenuous on that final point – I know some new tools are coming. But without a better understanding of what Specification/Behaviour Driven Design is really about, the new tools will be pointless.

Back from Glastonbury, and it seems people have been busy in my absence. A while back I integrated some Javascript from “Stuart Langridge”:http://www.kryogenix.org/ which parses Textile formatted text into HTML – I used this code to create a live preview for commentors (which itself was inspired by the HTML live preview on “Jon Hick’s”:http://www.hicksdesign.co.uk/journal/ site). Jeff Minard spotted my “original post(magpiebrain – Live textile comment previews)”:http://www.magpiebrain.com/archives/2004/05/18/preview on the subject, and “took it upon himself”:http://www.creatimation.net/journal/textile-live-preview to add support for some aspects of Textile formatting that were missing from Stuart’s original script prior to integrating it into his own very nice blog (another site that has encouraged me to start a minor design overhaul). When I get a spare moment, I’ll certainly be integrating Jeff’s changes into this site.

Any initial impressions are that its very nice indeed. I uninstalled 0.8 before installing “as advised(Firefox 0.9 release notes)”:http://www.mozilla.org/products/firefox/releases/, and was a little disappointed to see that it lost my settings. This however was a minor inconvenience (most of my bookmarks are in “del.icio.us”:http://del.icio.us for example) – the extension manager is fantastic, and it was a breeze to reinstall my favourite “Web Developer”:http://update.mozilla.org/extensions/moreinfo.php?application=firefox&id=60&vid=63 extension (which has added even more useful features, such as improved CSS reports and a “page speed report”:http://www.websiteoptimization.com/cgi-bin/wso/wso.pl?url=http%3A//www.magpiebrain.com/. Now all I have to do is wait for the “Tab Browser Extension”:http://white.sakura.ne.jp/~piro/xul/_tabextensions.html.en to support the new API…

I woke up this morning to a pleasant arrival in my inbox – no, not the latest Nigeria scam, instead a communication from “Sixapart”:http://www.sixapart.com/, makers of “Movable Type”:http://www.moveabletype.org. Firstly they informed me that my Personal Edition has been upgraded for free to an Unlimited Personal Edition (so I can now have as many authors as I like), but have also explained that the standard Personal Edition now allows an unlimited number of blogs too (previously you were limited to five authors).

When the original licensing structure was announced many bloggers got a “little histerical”:http://diveintomark.org/archives/2004/05/14/freedom-0 – I instead decided to wait it out and see how Six Apart dealt with the criticism. Six Apart listened, reacted, and the net result is that the new license scheme was to my liking. I paid for a license an am very happy with it. The fact that they’ve further rewarded me for my support is icing on an already quite nice cake.

You’ve found a project which interests you. It looks in fact like it might be incredibly useful. You start to play around with the code. You have some questions, find some bugs. You post queries and bug reports. Nothing happens. You find some more problems – some architectural issues for example, which result in queries you’d like answered. Still no reply. You even send in patches – still nothing. You like the idea of the project, just not the current implementation or the way its being run – so when do you make the decision to fork, or even re-implement it?

I’m currently writing a UI layer for Naked Objects. I’m using a TDD approach and have also used the development as an excuse to give “JMock”:http://www.jmock.org/ a go. It being my first experience using a proper Mock Objects API (I have used the very handy “MockRunner”:http://mockrunner.sourceforge.net/, which Mocks application frameworks rather than objects) its been slow going at the start. Things have now started to click into place mentally and JMock is at last making some sense.

Today however I hit a really annoying problem. I’m developing a simple @FrameFactory@, who when given a @NakedClass@ will return a window showing a list of instances. The code looks like this:


public class FrameFactory {
  public FrameFactory(FactoryDependancy dependancy) {
    ...
  }

  public Component makeInstanceList(NakedClass clazz) {
    //make an instance - this is what I want to test
  }
}

Continue reading…