atian25
atian25

Reputation: 4256

groovy way of if(var) fn(var)

i want to know is there any groovier way of code below:

def dataList = OperLog.createCriteria().list(max:params.max, offset:params.offset) {
    if(params.relationId){
      eq('relationId',params.long('relationId'))
    }
    order(params.sort, params.order)
}

such as someVar?.someMethod is there any sugar of don't call a method where it's params is null

Upvotes: 2

Views: 295

Answers (2)

atian25
atian25

Reputation: 4256

is there any groovier style of this?

def list = [vo1,vo2,vo3]
list.each{
   someMethod(it)
}

just like

list*.toString()

Upvotes: 0

tim_yates
tim_yates

Reputation: 171084

You could do:

params.relationId?.with { rid ->
  println rid
}

And the code inside the with block will not be executed if params.relationId is null...

However, I'd argue that your original code is more obvious in its intentions, and you won't have to try and work out what it is doing when you come to review it at a later date ;-)

Upvotes: 5

Related Questions