-2

I always considered returning a null object to be bad practice for a method call that is to be responded to. Lets say

Int GetFieldByID( String IDString );

so

Int m_MyReturnedID = GetFieldByID( "myGamerTag" );

Now, I'd assume that if the ID could not be found, we might return -1 and then handle that instead of returning a null object since that might in fact mean something went wrong internally. Anyway, I was just over on the XNA developer website and was looking at this:

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.net.networksession.findgamerbyid.aspx

As you can see:

Return Value

Network gamer matching the requested ID, or null if no matching gamer was found.

Am I wrong in my preconception that returning a null object like this is bad practice? I suddenly have my doubts because I would assume that M$ developers should definitely know what they are doing for a service like XNA/Live etc, especially after various iterations.

Thanks

Gary Paluk
  • 1,038
  • 1
  • 14
  • 28
  • 1
    why returning -1 is better than null? – Mauricio Jun 24 '11 at 02:34
  • 1
    The example you're quoting doesn't match your question. The actual link says nothing about returning `int` or `null` - it says it returns `NetworkGamer`, or `null` if the specified ID isn't found. There's a difference between `int` and an Object, which is what `NetworkGamer` represents AFAICT. – Ken White Jun 24 '11 at 03:19
  • @Ken White - My 1st example didn't match because it was a pseudo example which I then followed up with the XNA documentation link as a separate example. I know the difference between an Object and an intrinsic data type. Lets try answering the question. @Maurico I don't know, hence my question. (Because it's of the same type?) – Gary Paluk Jun 24 '11 at 05:46
  • Let's try asking a question then. You might want to also work on your manners; it's very impolite to be rude and insulting to people you're asking to give you help, especially when that help is free. Please take a few minutes to read the [FAQ](http://stackoverflow.com/faq), particularly the section pertaining to how you should behave toward others. Snark and rudeness don't work here, and usually aren't tolerated; it's what helps keep this site such a useful resource. Thanks. – Ken White Jun 25 '11 at 15:08

1 Answers1

1

There are two different situations you're describing here:

  • Returning a value type, which cannot be null (as in your example)
  • Returning a reference type (as in the API you actually linked) - and this applies to Nullable types too.

A value type cannot be null, which is why you might return -1 for a "not found" situation. You often cannot return 0 because most of the time zero is a valid value. In this case -1 is a sentinel value.

Now a variable that refers to a reference type (class) object can be null (ie: not referencing any object). However you could also create your reference type such that it has a "sentinel state" that represents "not valid".

This is, of course, a complete pain in the ass. Don't do it.

Instead of checking this:

if(foo != null) ...

You'd be doing this:

if(foo != null && foo.IsValid) ...

Which is more work, and even if something enforced non-null-ness, you'd still have to do this:

if(foo.IsValid) ...

Which doesn't really win you anything. Plus you'd have to write all the extra code to handle the extra bit of state in every single class that you write.

The actual problem with your theory is your idea that a method should return null if "something went wrong internally". In that case the method should almost certianly throw an exception.

Andrew Russell
  • 26,924
  • 7
  • 58
  • 104