473,804 Members | 4,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between returning null and array with zero elements

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
10 4741
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

"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
On 11 Apr 2006 03:51:51 -0700, "Jon Skeet [C# MVP]" <sk***@pobox.co m> 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
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
On 11 Apr 2006 04:54:14 -0700, "Jon Skeet [C# MVP]" <sk***@pobox.co m> 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
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.co m> 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

"Sek" <se****@gmail.c om> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.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
ArgumentNullExc eption and
NotSupportedExc eption
the ArrayList.Capac ity Property throws
ArgumentOutOfRa ngeException
Even some getters throw Exceptions
the FileInfo.Length Property throws
IOException and
FileNotFoundExc eption

the DirectoryInfo.P arent Property throws
SecurityExcepti on

Bill
Bill
Apr 12 '06 #8
On 11 Apr 2006 18:46:07 -0700, "Sek" <se****@gmail.c om> 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
inappropriat e to throw exception from a Property?

Otis Mukinfus wrote:
On 11 Apr 2006 04:54:14 -0700, "Jon Skeet [C# MVP]" <sk***@pobox.co m> 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.
ArgumentExcepti on 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
Sek
bill,

thanks for the specific examples.

Apr 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
11501
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 an object... the function is like so: function calc_total() { var x,i,base,margin,total,newmargin,newtotal; base = document.frmKitAmount.txtTotalKitValue.value; margin = document.frmKitAmount.margin.value/100;
31
3539
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 function variables, for example, typedef struct {
3
7663
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
1
3331
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 for my first element but my subsequent printf's will return garbage. Can someone let me know what I am doing wrong. Thanks in advance. -jlewis
29
3772
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 compiler dependent (and that NULL might be anything, but it is always NULL). The source snippet is below. The question is: - When I use calloc to allocate a block of memory, preinitialising it to zero, is this equivalent (and portable C) to...
3
5888
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 _strVal = "" thanks for enlightening me on this.
17
3265
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
45
7401
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.