[ 
https://issues.apache.org/jira/browse/LANG-534?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Levon Karayan updated LANG-534:
-------------------------------

    Description: 
There are APIs that I've come across that return <code>null</code> Arrays in 
the event where there are no results.  Often these APIs correctly throw 
exceptions when there is an "exceptional event", but no results isn't 
exceptional, and it often shouldn't be.   This causes the programmer to make 
extra tests for null throughout the code to deal with the null case, and 
sometimes these null cases are added after a customer searched for gobleygook 
and got a NullPointerException.  It's just far cleaner/safer to convert these 
null arrays to empty arrays.

Another benefit to this method is that if the array being passed in is actually 
already an empty array, it will swap the pointer for the <code>static 
final</code> in the ArrayUtils class to help decrease memory fragmentation.

e.g.

BEFORE:

try
{
  results = customer.getResults(query);
} catch ( IOException ioex ) {
  //  ...
}
if ( null == results )
{
   results = new int[0]{};
}
// do stuff

AFTER
try
{
  results = ArrayUtils.nullToEmpty(customer.getResults(query));
} catch ( IOException ioex ) {
  //  ...
}
// do stuff




  was:
There are APIs that I've come across that return <code>null</code> Arrays in 
the event where there are no results.  Often these APIs correctly throw 
exceptions when there is an "exceptional event", but no results isn't 
exceptional.   This causes the programmer to make extra tests for null 
throughout the code to deal with the null case, and sometimes these null cases 
are added after a customer searched for gobleygook and got a 
NullPointerException.  It's just far cleaner/safer to convert these null arrays 
to empty ones.

Another benefit to this method is that if the array being passed in is actually 
already an empty array, it will swap the pointer for the <code>static 
final</code> in the ArrayUtils class to help with memory fragmentation.

e.g.

BEFORE:

try
{
  results = customer.getResults(query);
} catch ( IOException ioex ) {
  //  ...
}
if ( null == results )
{
   results = new int[0]{};
}
// do stuff

AFTER
try
{
  results = ArrayUtils.nullToEmpty(customer.getResults(query));
} catch ( IOException ioex ) {
  //  ...
}
// do stuff





> ArrayUtils should have method to conver null arrays to empty ones to help 
> with Defensive coding
> -----------------------------------------------------------------------------------------------
>
>                 Key: LANG-534
>                 URL: https://issues.apache.org/jira/browse/LANG-534
>             Project: Commons Lang
>          Issue Type: Improvement
>            Reporter: Levon Karayan
>            Priority: Trivial
>         Attachments: 20090920LevonArrayUtils-nullToEmpty.patch
>
>   Original Estimate: 0.17h
>  Remaining Estimate: 0.17h
>
> There are APIs that I've come across that return <code>null</code> Arrays in 
> the event where there are no results.  Often these APIs correctly throw 
> exceptions when there is an "exceptional event", but no results isn't 
> exceptional, and it often shouldn't be.   This causes the programmer to make 
> extra tests for null throughout the code to deal with the null case, and 
> sometimes these null cases are added after a customer searched for gobleygook 
> and got a NullPointerException.  It's just far cleaner/safer to convert these 
> null arrays to empty arrays.
> Another benefit to this method is that if the array being passed in is 
> actually already an empty array, it will swap the pointer for the 
> <code>static final</code> in the ArrayUtils class to help decrease memory 
> fragmentation.
> e.g.
> BEFORE:
> try
> {
>   results = customer.getResults(query);
> } catch ( IOException ioex ) {
>   //  ...
> }
> if ( null == results )
> {
>    results = new int[0]{};
> }
> // do stuff
> AFTER
> try
> {
>   results = ArrayUtils.nullToEmpty(customer.getResults(query));
> } catch ( IOException ioex ) {
>   //  ...
> }
> // do stuff

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to