method that returns either MatchCollection or nothing?

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

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?
 
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))
 
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.
 
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.
 
How do you return null collections?


Jack Jackson said:
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.
 
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).
 
Ok...


Jack Jackson said:
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).
 
Back
Top