Is there a better way to write this null check, and a non-empty check, in groovy?

Jay Bose picture Jay Bose · Jun 23, 2013 · Viewed 192.8k times · Source

I need to perform a null/empty check on some code, before performing some logic. I have item below, because I feel !members?.empty is not correct.

Is there a groovier way to write the following?

if (members && !members.empty) {
    // Some Work
}

Answer

dmahapatro picture dmahapatro · Jun 23, 2013

There is indeed a Groovier Way.

if(members){
    //Some work
}

does everything if members is a collection. Null check as well as empty check (Empty collections are coerced to false). Hail Groovy Truth. :)