Let "for" loop treat "null" as empty List ?

  • Follow


I have the following code:

public void myiteration(List <MyObject> mylist) {

  if (mylist  != null) {
     for ( MyObject mo : mylist) {
         ...  }
     }

As you can see I have to check at first whether the passed list is null or not.
If the is null and have no "if" check on the null value then the prog would crash at the "for" loop.

Ok, it works as shown before.

However I would appreciate to be able to treat an null value as an empty list.
I want to omit the leading, wrapping "if" check.

Is this possible somehow?

Maybe with a workaround like:

for (MyObject mo : mylist; (mylist = NOT(null)))
 
Robin

0
Reply rob 1/25/2011 10:07:05 AM

On 25/01/2011 11:07, Robin Wenger wrote:
> I have the following code:
>
> public void myiteration(List<MyObject>  mylist) {
>
>    if (mylist  != null) {
>       for ( MyObject mo : mylist) {
>           ...  }
>       }
>
> As you can see I have to check at first whether the passed list is null or not.
> If the is null and have no "if" check on the null value then the prog would crash at the "for" loop.
>
> Ok, it works as shown before.
>
> However I would appreciate to be able to treat an null value as an empty list.
> I want to omit the leading, wrapping "if" check.
>
> Is this possible somehow?
>
> Maybe with a workaround like:
>
> for (MyObject mo : mylist; (mylist = NOT(null)))

Well you /can/ make something like

if(mylist == null) {
   mylist = Collections.emptyList();
}

before the loop. Same difference really.

I think you should really make a best effort of not sending null Lists 
as parameter of the method in the first place, though. That way, the 
method can correctly throw a NullPointerException when that happens, as 
it must not happen.

--
Mayeul
0
Reply Mayeul 1/25/2011 10:35:39 AM


On 25.1.2011 12:07, Robin Wenger wrote:
> I have the following code:
> 
> public void myiteration(List <MyObject> mylist) {
> 
>   if (mylist  != null) {
>      for ( MyObject mo : mylist) {
>          ...  }
>      }
> 
> As you can see I have to check at first whether the passed list is null or not.
> If the is null and have no "if" check on the null value then the prog would crash at the "for" loop.
> 
> Ok, it works as shown before.
> 
> However I would appreciate to be able to treat an null value as an empty list.
> I want to omit the leading, wrapping "if" check.
> 
> Is this possible somehow?
> 
> Maybe with a workaround like:
> 
> for (MyObject mo : mylist; (mylist = NOT(null)))
>  
> Robin
> 

Lazy arent ya ;)

    myList = myList == null ? new ArrayList<MyObject>() : mylist ;
    for (MyObject obj : myList)
    {

Something you just have to write...Be it if or something other...

-- 

Q:	How many lawyers does it take to change a light bulb?
A:	You won't find a lawyer who can change a light bulb.  Now, if
	you're looking for a lawyer to screw a light bulb...
0
Reply Donkey 1/25/2011 11:59:24 AM

On 01/25/2011 05:07 AM, Robin Wenger wrote:
> Maybe with a workaround like:
>
> for (MyObject mo : mylist; (mylist = NOT(null)))

If you want to minimize extra lines:

public static <T> List<T> denullify(List<T> list) {
   return list == null ? Collections.emptyList<T>() : list;
}

....

for (MyObject mo : denullify(mylist))

Alternatively, you could put that conditional in the for statement 
directly, but it might be long for your tabulation requirements.


Also, what is up with people posting to cljp and cljh, with followups 
set to only cljh? It makes it confusing to see no responses...

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth
0
Reply Joshua 1/25/2011 1:00:41 PM

Robin Wenger wrote:
>> Maybe with a workaround like:
>>
>> for (MyObject mo : mylist; (mylist =3D NOT(null)))
>

This is not an improvement over suggestions like Joshua's.

Joshua Cranmer wrote:
> If you want to minimize extra lines:
>
> public static <T> List<T> denullify(List<T> list) {
> =A0 =A0return list =3D=3D null ? Collections.emptyList<T>() : list;
>
> }
>
> ...
>
> for (MyObject mo : denullify(mylist))
>
> Alternatively, you could put that conditional in the for statement
> directly, but it might be long for your tabulation requirements.
>

Instead of relying on the language to enforce invariants for you, you
can use the language to help enforce invariants for you with the
'assert' keyword.

Nothing removes from the programmer the responsibility to enforce
invariants.  One way or t'other you have to handle the 'null' case.
Often effective is to prevent the null case.  Then you can have code
like:

  assert things !=3D null;
  for ( Thing thing : things )
  {
    ...
  }

This is better on a ton of levels.

> Also, what is up with people posting to cljp and cljh, with followups
> set to only cljh? It makes it confusing to see no responses...
>

Yeah, OP and others, don't do that!  It's not like people aren't
asking you over and over and over to stop or anything.  You're asking
for help but flouting the etiquette - not very smart diplomacy nor
good manners, wouldn't you agree?
 Do you wish to antagonize those from whom you request help?  Do you
wish to be rude?

This is Usenet - you can be rude directly without playing header
games.

--
Lew
0
Reply Lew 1/25/2011 5:32:00 PM

Robin Wenger <rob@wenger.net> wrote:
>I have the following code:
>
>public void myiteration(List <MyObject> mylist) {
>
>  if (mylist  != null) {
>     for ( MyObject mo : mylist) {
>         ...  }
>     }
>As you can see I have to check at first whether the passed list is null or not.

What is your contract with the caller?  There are a few cases where you'd like
null to be considered an empty list, but most of the time it indicates an
error somewhere up the chain.

You're safer with:
  if (mylist == null) {
    throw new IllegalArgumentException("must specify list to iterate.");
  }

or, at a code/package/conceptual boundary where you've documented that null
and empty are the same thing for convenience, 
  if (mylist == null) {
    mylist = Collections.emptyList();
  }

and then feel free to pass mylist into your more private library classes,
which can treat null as the error it is.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>  


0
Reply dagon 1/25/2011 8:51:49 PM

On 25/01/2011 18:32, Lew allegedly wrote:
> Robin Wenger wrote:
<snip />
>> for (MyObject mo : denullify(mylist))
>>
>> Alternatively, you could put that conditional in the for statement
>> directly, but it might be long for your tabulation requirements.
>>
>
> Instead of relying on the language to enforce invariants for you, you
> can use the language to help enforce invariants for you with the
> 'assert' keyword.
>
> Nothing removes from the programmer the responsibility to enforce
> invariants.  One way or t'other you have to handle the 'null' case.
> Often effective is to prevent the null case.  Then you can have code
> like:
>
>    assert things != null;
>    for ( Thing thing : things )
>    {
>      ...
>    }
>
> This is better on a ton of levels.

Better yet: I assume you got that instance from a method which returned
a Collection. A method returning a Collection ought never to return null
unless it is necessary and is clearly indicated in its contract (e.g.
Javadoc).
The latter only makes sense if a null return value and an empty
collection represent different method results. If that's the case, then
you absolutely /have to/ handle the null case.
Otherwise, modify the method in question never to return null, but
java.util.Collections#emptySomething instead. If you can't modify it,
create a wrapper in your code for it.
Do not expect Java to provide you with syntactic sugar for things like
this. Or actually, dread the day might come when Java will provide you
syntactic sugar for things like this.
There are many cases where the Java programming language forces you to
handle things manually. In hardly *any* of these cases does it *not* 
make sense.

>
>> Also, what is up with people posting to cljp and cljh, with followups
>> set to only cljh? It makes it confusing to see no responses...
>>
>
> Yeah, OP and others, don't do that!  It's not like people aren't
> asking you over and over and over to stop or anything.  You're asking
> for help but flouting the etiquette - not very smart diplomacy nor
> good manners, wouldn't you agree?
>   Do you wish to antagonize those from whom you request help?  Do you
> wish to be rude?
>
> This is Usenet - you can be rude directly without playing header
> games.

Bloody fucking yeah.

df.
0
Reply Daniele 1/25/2011 11:40:06 PM

6 Replies
243 Views

(page loaded in 0.181 seconds)

Similiar Articles:













7/22/2012 2:23:37 PM


Reply: