开发者

Collection should return null or empty upon a Db load ending with no records found?

开发者 https://www.devze.com 2023-01-04 08:21 出处:网络
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?

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

  • null

Is there any consensus on this?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号