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? 10 4709
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
"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
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
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
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
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
"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
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
bill,
thanks for the specific examples.
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: cwizard |
last post by:
I'm calling on a function from within this form, and there are values set
but every time it gets called I get slammed with a run time error...
document.frmKitAmount.txtTotalKitValue is null or not...
|
by: vp |
last post by:
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:
"memset( &p, 0, sizeof(p));" instead of "p = NULL;"
The reason I ask is I have an array of structure of N...
|
by: sathyashrayan |
last post by:
The standard confirms that the following initialization of a struct
struct node
{
---
---
}
struct node var = {NULL};
|
by: john |
last post by:
Relatively new to C coding, so any help would greatly be appreciated.
I'm having problems try to return my string array from my parsing
function. When I do a printf I am getting the correct value...
|
by: Jason Curl |
last post by:
I've been reading this newsgroup for some time and now I am thoroughly
confused over what NULL means.
I've read a NULL pointer is zero (or zero typecast as a void pointer),
others say it's...
|
by: Asha |
last post by:
greetings, i have some questions below, what are the differences between
private string _strVal = string.Empty; and _strVal = null;
does the string.Empty; allocate memory for it? how about...
|
by: I.M. !Knuth |
last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until
I started goofing around with this:
...
|
by: anto frank |
last post by:
hi friends,
is ther any difference in array in c and array in c++?
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |