It's not much easier a conditional expression (or even the elvis operator)?

```
def m() {
    def r = callSomeMethod()
    return r != null ? r : theDefaultResult
}
```


On Sat, Jul 25, 2020 at 8:56 PM Daniel Sun <sun...@apache.org> wrote:

> Hi all,
>
>      We always have to check the returning value, if it match some
> condition, return it. How about simplifying it? Let's see an example:
>
> ```
> def m() {
>     def r = callSomeMethod()
>     if (null != r) return r
>
>     return theDefaultResult
> }
> ```
>
> How about simplifying the above code as follows:
> ```
> def m() {
>     return? callSomeMethod()
>     return theDefaultResult
> }
> ```
>
> Futhermore, we could make the conditional return more general:
> ```
> def m() {
>     return(r -> r != null) callSomeMethod() // we could do more checking,
> e.g. r > 10
>     return theDefaultResult
> }
> ```
>
>     Any thoughts?
>
> Cheers,
> Daniel Sun
>

Reply via email to