If statement Groovy代码简化和';如果';消除

If statement Groovy代码简化和';如果';消除,if-statement,groovy,If Statement,Groovy,我有以下代码: def avatar = avatarsService.avatar(logged, userId).get() def result if (avatar.success) { def url = avatar.content.avatarUrl if (url) { def content = contentForAvatar(url) result = conten

我有以下代码:

    def avatar = avatarsService.avatar(logged, userId).get()

    def result

    if (avatar.success) {
        def url = avatar.content.avatarUrl

        if (url) {
            def content = contentForAvatar(url)
            result = content ? prepareAvatarSuccessResponse(content, avatar.content.fileType) : prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)
        } else {
            result = prepareAvatarErrorResponse(NOT_FOUND)
        }
    } else {
        result = prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)
    }

    result

首先,我需要从
userService
获取头像描述,然后处理有效的内容(
success
)。我不喜欢这个结构?有人知道如何简化吗?

将您的
avatarsService.avatar(logged,userId).get()的输出反转,这样它就有了一个
错误
字段,而不是成功。此字段的值将是
未找到
内部服务器错误

def avatar = avatarsService.avatar(logged, userId).get()

def result = avatar.error ? prepareAvatarErrorResponse( avatar.error ) : 
                            prepareAvatarSuccessResponse(content, avatar.content.fileType)

反转您的
avatarsService.avatar(logged,userId).get()
的输出,这样它就有一个
错误
字段,而不是成功。此字段的值将是
未找到
内部服务器错误

def avatar = avatarsService.avatar(logged, userId).get()

def result = avatar.error ? prepareAvatarErrorResponse( avatar.error ) : 
                            prepareAvatarSuccessResponse(content, avatar.content.fileType)

我推荐基于行动图的解决方案,创建响应:

def avatar = avatarsService.avatar(logged, userId).get()
if (!avatar.success) {
        return prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)
}
def map = [
        [true, true] : {prepareAvatarSuccessResponse(content, avatar.content.fileType)},
        [true, false] : {prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)}].withDefault {prepareAvatarErrorResponse(NOT_FOUND)}
return map[[ (url = avatar.content.avatarUrl) as boolean, contentForAvatar(url) as boolean]]()

有些事情可以改进。

我推荐了基于行动图的解决方案,创建响应:

def avatar = avatarsService.avatar(logged, userId).get()
if (!avatar.success) {
        return prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)
}
def map = [
        [true, true] : {prepareAvatarSuccessResponse(content, avatar.content.fileType)},
        [true, false] : {prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)}].withDefault {prepareAvatarErrorResponse(NOT_FOUND)}
return map[[ (url = avatar.content.avatarUrl) as boolean, contentForAvatar(url) as boolean]]()

有些事情是可以改进的。

您似乎想要改变您的编程模式:它是

逻辑编程。 无论如何,我们可以基于强大的闭包:

ifElse(success){
    ifElse(avatar.content.avatarUrl){
         //thanks to delegate , "it" equals now to avatar.content.avatarUrl
         def content = contentForAvatar(it)
         result=content?prepareAvatarSuccessResponse(content, avatar.content.fileType) : prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)
    }{
         result = prepareAvatarErrorResponse(NOT_FOUND)
    }
}{
      result = prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)
}
助手方法:

def ifElse(def test,Closure c,Closure ifnot={}){
     if(test){
          c.delegate=test;
          c();
        }else{
         ifnot.delegate=test;
         ifnot();

    }
}
  • 这里有两个优点:

  • 闭包之间没有
    else

  • 基于传递给闭包的参数(通过委托),去掉一些var声明(def url)


您似乎想要改变您的编程范式:它是

逻辑编程。 无论如何,我们可以基于强大的闭包:

ifElse(success){
    ifElse(avatar.content.avatarUrl){
         //thanks to delegate , "it" equals now to avatar.content.avatarUrl
         def content = contentForAvatar(it)
         result=content?prepareAvatarSuccessResponse(content, avatar.content.fileType) : prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)
    }{
         result = prepareAvatarErrorResponse(NOT_FOUND)
    }
}{
      result = prepareAvatarErrorResponse(INTERNAL_SERVER_ERROR)
}
助手方法:

def ifElse(def test,Closure c,Closure ifnot={}){
     if(test){
          c.delegate=test;
          c();
        }else{
         ifnot.delegate=test;
         ifnot();

    }
}
  • 这里有两个优点:

  • 闭包之间没有
    else

  • 基于传递给闭包的参数(通过委托),去掉一些var声明(def url)


一个选项是使用Groovy
开关:

def avatar = avatarsService.avatar(logged, userId)
switch(avatar) {
    case {!it.success}:           return error(INTERNAL_SERVER_ERROR)
    case {!it.content.url}:       return error(NOT_FOUND)
    case {!it.retrieveContent()}: return error(INTERNAL_SERVER_ERROR)
    default:                      return success(avatar.retrieveContent(), avatar.content.fileType)
}

为了可读性,我将prepare方法重命名为just
error
success
。我还将方法
retrieveContent
添加到了头像中。如果以这种方式使用,请注意不要阅读内容两次。

一个选项是使用Groovy
开关:

def avatar = avatarsService.avatar(logged, userId)
switch(avatar) {
    case {!it.success}:           return error(INTERNAL_SERVER_ERROR)
    case {!it.content.url}:       return error(NOT_FOUND)
    case {!it.retrieveContent()}: return error(INTERNAL_SERVER_ERROR)
    default:                      return success(avatar.retrieveContent(), avatar.content.fileType)
}

为了可读性,我将prepare方法重命名为just
error
success
。我还将方法
retrieveContent
添加到了头像中。如果使用这种方式,请注意不要阅读内容两次。

Nice。好的。我会调查的。