By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

Difference between returning null and array with zero elements

P: n/a
Sek
I have a property that returns a ArrayList object.

On failure condition, is it right to return null or return an ArrayList
object with zero elements?

Apr 11 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Sek wrote:
I have a property that returns a ArrayList object.

On failure condition, is it right to return null or return an ArrayList
object with zero elements?


Well, are you writing the clients for this API? If so, what would be
more useful to you? Do you actually want to mask the failure at all -
would an exception be more appropriate?

Jon

Apr 11 '06 #2

P: n/a

"Jon Skeet [C# MVP]" wrote...
Sek wrote:

I have a property that returns a ArrayList object.

On failure condition, is it right to return null or return an ArrayList
object with zero elements?


Well, are you writing the clients for this API? If so, what would be
more useful to you? Do you actually want to mask the failure at all -
would an exception be more appropriate?


I would say that it depends on what "contract" the property should meet, but
in most cases I think that a zero-length ArrayList would be preferred over a
null. Returning a "null" causes more problems in more cases.

Take this eaxmple:

class Person
{
///<summary>
///A list of siblings.
///</summary>
ArrayList Siblings
{
get
{
// Say that you read the siblings of the person
// from a database, the client should probably expect
// an ArrayList, even if it's empty, not a null.
}
}
}

But Jon's question is very valid; what do you mean by "failure"? If it's
severe, the best solution would probably not be to mask it at all, but to
let it propagate to the caller.

// Bjorn A
Apr 11 '06 #3

P: n/a
On 11 Apr 2006 03:51:51 -0700, "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
Sek wrote:
I have a property that returns a ArrayList object.

On failure condition, is it right to return null or return an ArrayList
object with zero elements?


Well, are you writing the clients for this API? If so, what would be
more useful to you? Do you actually want to mask the failure at all -
would an exception be more appropriate?

Jon


But sometimes its not an exception that an ArrayList is empty. I would use an
exception only when an ArrayList is never allowed to be empty. Otherwise check
it for a count of zero before attempting to use it. I would prefer not to check
an ArrayList for null. If it's empty then I can add to it if I need to.

I do a lot of data scrubbing and backend work and I use ArrayLists, List<> and
Hashtable a lot and when they don't have data in them I return them with zero
elements. If their being empty is not good then I can throw an exception from
the caller or simply not execute the code that uses them.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Apr 11 '06 #4

P: n/a
Otis Mukinfus wrote:
But sometimes its not an exception that an ArrayList is empty.


Sure - but the OP specified that this is in a failure condition. The
option I'm highlighting is to throw an exception from the property
instead of returning an ArrayList at all. It could be that an empty
list is perfectly valid in normal conditions, but that failure
conditions shouldn't be masked by pretending that it's a normal
situation which returns no data.

My point is that each of the options (return null, return empty list,
throw exception) is valid in different cases - it depends on what the
OP's callers will find most useful.

Jon

Apr 11 '06 #5

P: n/a
On 11 Apr 2006 04:54:14 -0700, "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
Otis Mukinfus wrote:
But sometimes its not an exception that an ArrayList is empty.


Sure - but the OP specified that this is in a failure condition. The
option I'm highlighting is to throw an exception from the property
instead of returning an ArrayList at all. It could be that an empty
list is perfectly valid in normal conditions, but that failure
conditions shouldn't be masked by pretending that it's a normal
situation which returns no data.

My point is that each of the options (return null, return empty list,
throw exception) is valid in different cases - it depends on what the
OP's callers will find most useful.

Jon


I agree 100% Jon. I wasn't inferring your answer was wrong.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Apr 12 '06 #6

P: n/a
Sek
Thanks a lot folks.

I understand that the return value depends on the severity of the
failure.

Even in that case, is it appropriate to throw an exception from inside
a Property.
I was casually searching for any Property in .NET framework that throws
exception. I couldn't come across one. Does it mean that, its
inappropriate to throw exception from a Property?

Otis Mukinfus wrote:
On 11 Apr 2006 04:54:14 -0700, "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
Otis Mukinfus wrote:
But sometimes its not an exception that an ArrayList is empty.


Sure - but the OP specified that this is in a failure condition. The
option I'm highlighting is to throw an exception from the property
instead of returning an ArrayList at all. It could be that an empty
list is perfectly valid in normal conditions, but that failure
conditions shouldn't be masked by pretending that it's a normal
situation which returns no data.

My point is that each of the options (return null, return empty list,
throw exception) is valid in different cases - it depends on what the
OP's callers will find most useful.

Jon


I agree 100% Jon. I wasn't inferring your answer was wrong.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com


Apr 12 '06 #7

P: n/a

"Sek" <se****@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Thanks a lot folks.

I understand that the return value depends on the severity of the
failure.

Even in that case, is it appropriate to throw an exception from inside
a Property.
I was casually searching for any Property in .NET framework that throws
exception. I couldn't come across one. Does it mean that, its
inappropriate to throw exception from a Property?

check out the thread "Property or Function?"

This same topic is being hashed about in there.
I listed a few properties that throw exceptions in that thread
here are a few

The Hashtable.Item Property throws
ArgumentNullException and
NotSupportedException
the ArrayList.Capacity Property throws
ArgumentOutOfRangeException
Even some getters throw Exceptions
the FileInfo.Length Property throws
IOException and
FileNotFoundException

the DirectoryInfo.Parent Property throws
SecurityException

Bill
Bill
Apr 12 '06 #8

P: n/a
On 11 Apr 2006 18:46:07 -0700, "Sek" <se****@gmail.com> wrote:
Thanks a lot folks.

I understand that the return value depends on the severity of the
failure.

Even in that case, is it appropriate to throw an exception from inside
a Property.
I was casually searching for any Property in .NET framework that throws
exception. I couldn't come across one. Does it mean that, its
inappropriate to throw exception from a Property?

Otis Mukinfus wrote:
On 11 Apr 2006 04:54:14 -0700, "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
>Otis Mukinfus wrote:
>> But sometimes its not an exception that an ArrayList is empty.
>
>Sure - but the OP specified that this is in a failure condition. The
>option I'm highlighting is to throw an exception from the property
>instead of returning an ArrayList at all. It could be that an empty
>list is perfectly valid in normal conditions, but that failure
>conditions shouldn't be masked by pretending that it's a normal
>situation which returns no data.
>
>My point is that each of the options (return null, return empty list,
>throw exception) is valid in different cases - it depends on what the
>OP's callers will find most useful.
>
>Jon


I agree 100% Jon. I wasn't inferring your answer was wrong.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com


You can and should throw exceptions from properties when it makes sense.
ArgumentException for setters is the first thst comes to mind. You'd want to
throw an exception if someone tried to set an int property to a string, etc.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Apr 12 '06 #9

P: n/a
Sek
bill,

thanks for the specific examples.

Apr 13 '06 #10

P: n/a

"Otis Mukinfus" <ph***@emailaddress.com> wrote in message
news:02********************************@4ax.com...
On 11 Apr 2006 18:46:07 -0700, "Sek" <se****@gmail.com> wrote:

[cut]
You can and should throw exceptions from properties when it makes sense.
ArgumentException for setters is the first thst comes to mind. You'd want
to
throw an exception if someone tried to set an int property to a string,
etc.


The compiler wouldn't let you.
Apr 14 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.