tags:

views:

200

answers:

8

So let's say you have a function that returns a date

Date myFunc(paramA, paramB){ //conditionally return a date?

}

So is it appropriate to return null from this function? This seems ugly because it forces clients to check for null.

The "null object" pattern is an implementation pattern that addresses this concern.
I'm not a huge fan of the null object pattern, but yes it makes sense to always return a list, even if is empty than to return a null.... However, say in Java, a null date would be one that is cleared and has the year 1970. So what does everyone think is the best implementation pattern here?

+5  A: 

null is quite acceptable. However if you want to return null on an error, consider throwing an exception instead.

GavinCattell
If having no date is a valid condition, I would not have it throw an exception.
18Rabbit
+2  A: 

If it is possible a date won't be found then the null makes sense. Otherwise you end up returning some magical date (like the 1970 epoch) that will frustrate people hooking into the function far more than just getting a null returned.

Document that it could return null, however...

tloach
A: 

If its not a performance hit I like to have an explicit query method and then use exceptions:

if(employee.hasCustomPayday()) {
    //throws a runtime exception if no payday
    Date d = emp.customPayday();
}
Garth Gilmour
Would you really use RuntimeException here? I know that's the norm in C#, so maybe that's the kinda of example you're presenting. However, RuntimeExceptions should be reserved for cases where you can't do much with the cause, like a database disappearing, for example.
Spencer K
If the developer has already done the query then they shouldnt be forced to put in a try ... catch. It kinda wrecks the idiom. If an exception is thrown even though the query returned true then it probably deserves a runtime exception.
Garth Gilmour
+1  A: 

I'm not a fan of the null object pattern.

If null is a valid and intended return value, then return it. If it's caused by an error condition, an exception would make more sense.

Sometimes, the real problem is the method should be returning a more complex type that does represent more information. In those cases it's easy to fall into a trap and return some basic type, plus some special magic values to represent other states.

Marc Hughes
I generally agree, however it can make sense to return an object if it exists, otherwise return null. So long as those are the only options that is a generally accepted practice.
tloach
+7  A: 

The null object pattern is not for what you are trying to do. That pattern is about creating an object with no functionality in it's implementation that you can pass to a given function that requires an object not being null. An example is NullProgressMonitor in Eclipse, which is an empty implementation of IProgressMonitor.

If you return a "null" date, like 1970, your clients will still need to check if it's "null" by seeing if it's 1970. And if they don't, misbehaviour will happen. However, if you return null, their code will fail fast, and they'll know they should check for null. Also, 1970 could be a valid date.

You should document that your method may return null, and that's it.

asterite
Very interesting answer and hence part of why I asked the question. But alot of people seem to interpet the null object pattern as was asked in my question. Hence why I posted it...Returning null conditionally feels smelly, in that its like introducing a side effect...
DanielHonig
As long as you document what your method returns, your code will be correct if you document that it could return null. Callers that experience an NPE without checking the Date returned are then incorrect because you explicitly documented the behavior of your method.
MetroidFan2002
A: 

Use exceptions if this is not a scenario that should usually happen.

Otherwise, (if this is for example an end-date for an event), just return null.

Please avoid magic values in any case ;)

Luk
A: 

You could try using an output parameter

boolean MyFunction( a,b,Date c)
{
  if (good) 
     c.SetDate(....);
  return good;

}

Then you can call it

Date theDate = new Date();
if(MyFunction(a, b ,theDate ) 
{
   do stuff with C
}

It still requires you to check something, but there isn't a way of avoiding some checking in this scenario.

Although SetDate is deprecated, and the Calendar implementation is just ugly.

Stupidest API change Sun ever did.

chris
Yeah I heard a rumor about moving to a JODA based design in Java 7?
DanielHonig
That would be sweet. The current implementation violates rule one of framework design: "Make the common easy, and the uncommon possible."
chris
You also have to make sure the Date parameter itself is not null, then throw a NPE if it is, which is just more code on top of it all.
Spencer K
Fair enough, but the original question was a bit contrived as well. He should just return null and check it. When people complicate the question, you get complicated answers.
chris
Agreed the question was contrived. I just wondered if perhaps I was missing a better way.
DanielHonig
+1  A: 

It seems like the expected results from this method is a Date, or none found. The none found case is typically represented by returning null. Although some would use an exception to represent this case, I would not (as it is a expected result and I have never been a fan of processing by exception).

The Null object pattern is not appropriate for this case, as has been stated. In fact, from my own experience, it is not appropriate for many cases. Of course, I have some bias due to some experience with it being badly misused ;-)

Robin