views:

21

answers:

1

Hi,

If you have a method that queries the Db and returns a collection populated with the records found, what should be returned upon no records found?

  • A new collection with .Count == 0 or

  • null

Is there any consensus on this?

Or returning null and returning an empty collection should have different meanings?

Thanks

+3  A: 

You should return an empty collection. That will avoid you to check every time that you got a null as return.

You will always have a code like (abstract code):

for index = 0 to collection.size
  // do operations

instead of:

if collection != null 
  for index = 0 to collection.size
    // do operations

Additionally, you could extend this to the NULL Object pattern if you need more complex behavior from your collection.

dpb
@dpb: I think that is a very good point.
burak ozdogan