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

R

Robin Wenger

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
 
J

Joshua Cranmer

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...
 
L

Lew

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

Joshua said:
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.

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.
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.
 
D

Daniele Futtorovic

Robin Wenger wrote:

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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top