magpiebrain

Sam Newman's site, a Consultant at ThoughtWorks

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…

Advertisements

4 Responses to “Fail early, fail fast and fail right”

  1. diathesis

    I’ve tended to favor _UnsupportedOperationException_ for this. I think my IDE method stubs are usually set to something like:

    ==

    throw new UnsupportedOperationException(“Method stub not implemented.” );

    ==

    Reply
  2. Sam Newman

    A little Off topic, yes 🙂 I cleaned up your double post and fixed the (handy) link – I’m using “Textile”:http://textism.com/tools/textile/ for comment formatting – it stops erant HTML screwing up the blog layout but still lets people format their posts. To create a HREF link using textile, surround the link text with double quotes followed by a colon, then put the HREF itself.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS

%d bloggers like this: