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

Discussion in 'Java' started by Robin Wenger, Jan 25, 2011.

  1. Robin Wenger

    Robin Wenger Guest

    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
    Robin Wenger, Jan 25, 2011
    #1
    1. Advertising

  2. 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
    Joshua Cranmer, Jan 25, 2011
    #2
    1. Advertising

  3. Robin Wenger

    Lew Guest

    Robin Wenger wrote:
    >> Maybe with a workaround like:
    >>
    >> for (MyObject mo : mylist; (mylist = 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) {
    >    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.

    --
    Lew
    Lew, Jan 25, 2011
    #3
  4. 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.
    Daniele Futtorovic, Jan 25, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randall Parker
    Replies:
    1
    Views:
    576
    Kevin Spencer
    Oct 28, 2005
  2. Luigi Donatello Asero

    Let or not let the text float

    Luigi Donatello Asero, Jan 15, 2004, in forum: HTML
    Replies:
    6
    Views:
    464
    Steve R.
    Jan 15, 2004
  3. =?ISO-8859-1?Q?Gregory_Pi=F1ero?=

    Urllib.encode() - How does it Treat a List?

    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=, Jul 28, 2006, in forum: Python
    Replies:
    0
    Views:
    290
    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=
    Jul 28, 2006
  4. Replies:
    12
    Views:
    150
    Tad McClellan
    May 18, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    371
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page