BIRT 3.7
Written by: Michael Williams
Featured Refcardz: Top Refcardz:
  1. Scrum
  2. Apache Maven 2
  3. Essential MySQL
  4. Node.js
  5. Groovy
  1. jQuery Selectors
  2. Ajax
  3. Java
  4. Spring Config.
  5. Java Concurrency

Link Details

Link 126010 thumbnail
User 327967 avatar

By selikoff
via selikoff.net
Published: Oct 15 2008 / 09:06

Continuing on Jeanne’s theme of nulls, its a pet peeve of mine when I come across code that returns null arrays instead of empty arrays. The purpose of this post is to discuss some of the reasons why its a good practice to return empty arrays, including Collection objects or typed array.
  • 33
  • 0
  • 3423
  • 0

Comments

Add your comment
User 218789 avatar

eelmore replied ago:

0 votes Vote down Vote up Reply

Check for nulls in the code that consumes these things.

User 203717 avatar

chudak replied ago:

0 votes Vote down Vote up Reply

I disagree. Always return an empty collection unless there is an error. The blog is spot on.
,

User 330778 avatar

horza replied ago:

0 votes Vote down Vote up Reply

I disagree with the disagreement. I much prefer it when a code returns a NULL when there are no results as it is the current convention in all languages and libraries I have come across. Any error handling/exception is within the library/function itself and handled there. This then allows the syntactically easy boolean test. Though providing one item less information it still makes sense as error or no error there are still no results being returned and you have to deal with that. Also the last supposedly ambiguous example doesn't even make sense, as if convention dictates that either an empty set or an error sends back NULL you obviously do not need to test separately for the empty set. Overall I prefer consistency that will work across all code over a slightly superior technical solution which I have to remember is an exception and will become a gotchya for anybody that takes over my legacy code. If the revolution comes, and everybody including ISO/ANSI/etc dictates empty set for no results and null for errors then I will quite happily switch sides!

Phillip.

User 327967 avatar

selikoff replied ago:

0 votes Vote down Vote up Reply

Consistency that will work across all code? You're one of those people that writes files in Java as you would in C++, with no standard functions, and all in byte code, right? Java affords some useful notions such as Collections.EMPTY_LIST, which have definite uses, not returning a null pointer is one of them.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.