473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

method that returns either MatchCollection or nothing?

Is it safe to make a method that returns a match collection or nothing? or
is it better to just return a match collection and have the code outside the
method validate that match collection is empty or not?

Oct 15 '08 #1
10 2049
On 2008-10-15, Andy B <a_*****@sbcglobal.netwrote:
Is it safe to make a method that returns a match collection or nothing? or
is it better to just return a match collection and have the code outside the
method validate that match collection is empty or not?
Personally, I prefere when a method returns an empty collection then if it
returns nothing. Returning a null collection (Nothing in VB), usually
complicates the client code - because now you need to do checks to avoid a
NullReferenceException. For example (Warning Air Code!):

' method returns a null (nothing) collection
Dim theCollection As List<SomeEntity= GetCollection()
If Not theCollection Is Nothing Then
For Each e As SomeEntity In theCollection
' do stuff
Next
End If

' method returns an empty collection
For Each e As SomeEntity In GetCollection()
' do stuff
Next

Anyway, there you have it :)

--
Tom Shelton
Oct 15 '08 #2
So I shouldn't be doing something like:

dim matches as MatchCollection = ReturnMatches(stuff)

if matches is nothing
return nothing
else
return matches
Instead I should:

return (matches as MatchCollection = ReturnMatches(stuff))
"Tom Shelton" <to*********@comcastXXXXXXX.netwrote in message
news:QJ******************************@comcast.com. ..
On 2008-10-15, Andy B <a_*****@sbcglobal.netwrote:
>Is it safe to make a method that returns a match collection or nothing?
or
is it better to just return a match collection and have the code outside
the
method validate that match collection is empty or not?

Personally, I prefere when a method returns an empty collection then if it
returns nothing. Returning a null collection (Nothing in VB), usually
complicates the client code - because now you need to do checks to avoid a
NullReferenceException. For example (Warning Air Code!):

' method returns a null (nothing) collection
Dim theCollection As List<SomeEntity= GetCollection()
If Not theCollection Is Nothing Then
For Each e As SomeEntity In theCollection
' do stuff
Next
End If

' method returns an empty collection
For Each e As SomeEntity In GetCollection()
' do stuff
Next

Anyway, there you have it :)

--
Tom Shelton

Oct 16 '08 #3
On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>Is it safe to make a method that returns a match collection or nothing? or
is it better to just return a match collection and have the code outside the
method validate that match collection is empty or not?
Always returning a collection (empty if nothing found) may make it
less likely that calling code will be written incorrectly. If you
return Nothing and a caller fails to check for Nothing an exception
will occur.

If you always return a collection, then depending on how the method
works, returning an empty collection might mean that it will be
necessary to create an empty collection that would not have to be
created if Nothing was returned.

So there might be a trade-off between safer code and creating more
objects.

If it were me I would probably return Nothing, and define the method
as returning either Nothing or a non-empty collection so that callers
don't have to make two tests to check if anything was returned.
Oct 16 '08 #4
On 2008-10-16, Jack Jackson <jj******@cinnovations.netwrote:
On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>>Is it safe to make a method that returns a match collection or nothing? or
is it better to just return a match collection and have the code outside the
method validate that match collection is empty or not?

Always returning a collection (empty if nothing found) may make it
less likely that calling code will be written incorrectly. If you
return Nothing and a caller fails to check for Nothing an exception
will occur.

If you always return a collection, then depending on how the method
works, returning an empty collection might mean that it will be
necessary to create an empty collection that would not have to be
created if Nothing was returned.

So there might be a trade-off between safer code and creating more
objects.

If it were me I would probably return Nothing, and define the method
as returning either Nothing or a non-empty collection so that callers
don't have to make two tests to check if anything was returned.
How often do you check to see if a collection is empty? Really... In most
cases, your going to loop over it and do stuff. You don't have to check for
an empty collection to loop, but you do have to check for a Nothing. Nope,
returning the empty collection is usually the right thing.

In general, returning the empty collection is going to make the calling code,
simpler and more maintainable.

--
Tom Shelton
Oct 16 '08 #5
On 2008-10-16, Andy B <a_*****@sbcglobal.netwrote:
So I shouldn't be doing something like:

dim matches as MatchCollection = ReturnMatches(stuff)

if matches is nothing
return nothing
else
return matches
Instead I should:

return (matches as MatchCollection = ReturnMatches(stuff))
ReturnMatches(stuff) should be returning a MatchCollection - even if there are
no matches.

--
Tom Shelton
Oct 16 '08 #6
How do you define it to return MatchCollection or nothing?
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:mo********************************@4ax.com...
On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>>Is it safe to make a method that returns a match collection or nothing? or
is it better to just return a match collection and have the code outside
the
method validate that match collection is empty or not?

Always returning a collection (empty if nothing found) may make it
less likely that calling code will be written incorrectly. If you
return Nothing and a caller fails to check for Nothing an exception
will occur.

If you always return a collection, then depending on how the method
works, returning an empty collection might mean that it will be
necessary to create an empty collection that would not have to be
created if Nothing was returned.

So there might be a trade-off between safer code and creating more
objects.

If it were me I would probably return Nothing, and define the method
as returning either Nothing or a non-empty collection so that callers
don't have to make two tests to check if anything was returned.

Oct 16 '08 #7
I meant that you document the method as operating that way.

However, Tom has a good point and I think always returning a
collection is probably better.

On Thu, 16 Oct 2008 06:38:56 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>How do you define it to return MatchCollection or nothing?
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:mo********************************@4ax.com.. .
>On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>>>Is it safe to make a method that returns a match collection or nothing? or
is it better to just return a match collection and have the code outside
the
method validate that match collection is empty or not?

Always returning a collection (empty if nothing found) may make it
less likely that calling code will be written incorrectly. If you
return Nothing and a caller fails to check for Nothing an exception
will occur.

If you always return a collection, then depending on how the method
works, returning an empty collection might mean that it will be
necessary to create an empty collection that would not have to be
created if Nothing was returned.

So there might be a trade-off between safer code and creating more
objects.

If it were me I would probably return Nothing, and define the method
as returning either Nothing or a non-empty collection so that callers
don't have to make two tests to check if anything was returned.
Oct 16 '08 #8
How do you return null collections?
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:2p********************************@4ax.com...
>I meant that you document the method as operating that way.

However, Tom has a good point and I think always returning a
collection is probably better.

On Thu, 16 Oct 2008 06:38:56 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>>How do you define it to return MatchCollection or nothing?
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:mo********************************@4ax.com. ..
>>On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:

Is it safe to make a method that returns a match collection or nothing?
or
is it better to just return a match collection and have the code outside
the
method validate that match collection is empty or not?

Always returning a collection (empty if nothing found) may make it
less likely that calling code will be written incorrectly. If you
return Nothing and a caller fails to check for Nothing an exception
will occur.

If you always return a collection, then depending on how the method
works, returning an empty collection might mean that it will be
necessary to create an empty collection that would not have to be
created if Nothing was returned.

So there might be a trade-off between safer code and creating more
objects.

If it were me I would probably return Nothing, and define the method
as returning either Nothing or a non-empty collection so that callers
don't have to make two tests to check if anything was returned.

Oct 16 '08 #9
I don't think "null collection" has any meaning.

You can return Nothing or you can return an empty collection (create a
collection but don't add anything to it).
On Thu, 16 Oct 2008 18:26:00 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>How do you return null collections?
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:2p********************************@4ax.com.. .
>>I meant that you document the method as operating that way.

However, Tom has a good point and I think always returning a
collection is probably better.

On Thu, 16 Oct 2008 06:38:56 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>>>How do you define it to return MatchCollection or nothing?
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:mo********************************@4ax.com ...
On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:

>Is it safe to make a method that returns a match collection or nothing?
>or
>is it better to just return a match collection and have the code outside
>the
>method validate that match collection is empty or not?

Always returning a collection (empty if nothing found) may make it
less likely that calling code will be written incorrectly. If you
return Nothing and a caller fails to check for Nothing an exception
will occur.

If you always return a collection, then depending on how the method
works, returning an empty collection might mean that it will be
necessary to create an empty collection that would not have to be
created if Nothing was returned.

So there might be a trade-off between safer code and creating more
objects.

If it were me I would probably return Nothing, and define the method
as returning either Nothing or a non-empty collection so that callers
don't have to make two tests to check if anything was returned.
Oct 17 '08 #10
Ok...
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:pm********************************@4ax.com...
>I don't think "null collection" has any meaning.

You can return Nothing or you can return an empty collection (create a
collection but don't add anything to it).
On Thu, 16 Oct 2008 18:26:00 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>>How do you return null collections?
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:2p********************************@4ax.com. ..
>>>I meant that you document the method as operating that way.

However, Tom has a good point and I think always returning a
collection is probably better.

On Thu, 16 Oct 2008 06:38:56 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:

How do you define it to return MatchCollection or nothing?
"Jack Jackson" <jj******@cinnovations.netwrote in message
news:mo********************************@4ax.co m...
On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>
>>Is it safe to make a method that returns a match collection or
>>nothing?
>>or
>>is it better to just return a match collection and have the code
>>outside
>>the
>>method validate that match collection is empty or not?
>
Always returning a collection (empty if nothing found) may make it
less likely that calling code will be written incorrectly. If you
return Nothing and a caller fails to check for Nothing an exception
will occur.
>
If you always return a collection, then depending on how the method
works, returning an empty collection might mean that it will be
necessary to create an empty collection that would not have to be
created if Nothing was returned.
>
So there might be a trade-off between safer code and creating more
objects.
>
If it were me I would probably return Nothing, and define the method
as returning either Nothing or a non-empty collection so that callers
don't have to make two tests to check if anything was returned.

Oct 17 '08 #11

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

Similar topics

10
by: Stuart McGraw | last post by:
I have a class A from a third party that I cannot change and is implemented in C. I derive my own class B from A and add a couple new methods and override a method. The problem is that A has a...
31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
16
by: 4Space | last post by:
I've hit something of a snag. Problem: In a DSP application we have data profiles with a varying number of points. We have a number of Discrete Fourier transforms that are optimised for a...
7
by: Cyphos | last post by:
Hi, I'm just learning how to use the XmlHttpRequest object. Very cool. However, I'm wondering how I can call a specific server-side method. For example, say I have a method defined as follows on...
1
by: Mortimer Schnurd | last post by:
Has anyone had any luck getting this CopyTo method to work? I can iterate through a MatchCollection and move each Match.Value to the System.Array without a problem. I just can't figure out why...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
6
by: Kini | last post by:
In the program below, when the control comes back to the Main program, a considerable amount of memory is still associated with the program. Is this normal? Is it due to repeated generation of...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.