0

有时我不喜欢在 kotlin 中嵌套 if else 。有没有办法写出更好的代码?我举一个例子,你可以选择任何其他例子。请分享您的反馈。

  suspend fun setStoreImages(call: ApplicationCall) {
    val storeImagesMapper = call.receive<ArrayList<StoreImageMapper>>()
    val employeeEmail = getEmailFromJWT(call)
    if (employeeEmail == null){
      call.respond(HttpStatusCode.BadRequest, "No employee email id passed")
    }else {
      val errorStr = StoreApis.createStoreImages(storeImagesMapper, employeeEmail)
      if (errorStr == null) {
        call.respond(HttpStatusCode.Created)
      } else {
        call.respond(HttpStatusCode.BadRequest, errorStr)
      }
    }
  }
4

3 回答 3

3

when 语句非常适合减少多个 if-else。errorStr在这种情况下,您可以employeeEmail使用?.let.

suspend fun setStoreImages(call: ApplicationCall) {
    val storeImagesMapper = call.receive<ArrayList<StoreImageMapper>>()
    val employeeEmail = getEmailFromJWT(call)
    val errorStr = employeeEmail?.let { StoreApis.createStoreImages(storeImagesMapper, it) }
    when {
        employeeEmail == null -> call.respond(HttpStatusCode.BadRequest, "No employee email id passed")
        errorStr == null -> call.respond(HttpStatusCode.Created)
        else -> call.respond(HttpStatusCode.BadRequest, errorStr)
    }
}

另一种策略是做早期回报而不是做其他陈述。


suspend fun setStoreImages(call: ApplicationCall) {
    val storeImagesMapper = call.receive<ArrayList<StoreImageMapper>>()
    val employeeEmail = getEmailFromJWT(call)
    if (employeeEmail == null) {
        call.respond(HttpStatusCode.BadRequest, "No employee email id passed")
        return
    }
    val errorStr = StoreApis.createStoreImages(storeImagesMapper, employeeEmail)
    if (errorStr == null) {
        call.respond(HttpStatusCode.Created)
        return
    }
    call.respond(HttpStatusCode.BadRequest, errorStr)
}
于 2021-05-23T17:52:07.180 回答
1

有多种方法可以做到这一点。

让我们举个例子。我们可以像这样使用返回键跳过 else 条件。现在,如果代码进入 if 条件,那么它将返回而不执行剩余的代码行。

suspend fun setStoreImages(call: ApplicationCall) {
    val storeImagesMapper = call.receive<ArrayList<StoreImageMapper>>()
    val employeeEmail = getEmailFromJWT(call)
    if (employeeEmail == null){
      call.respond(HttpStatusCode.BadRequest, "No employee email id passed")
      return
    }
      val errorStr = StoreApis.createStoreImages(storeImagesMapper, employeeEmail)
      if (errorStr == null) {
        call.respond(HttpStatusCode.Created)
      } else {
        call.respond(HttpStatusCode.BadRequest, errorStr)
      }
    }

如果 else 使用 when,我们可以再采取一步来删除剩余的部分。这在一个变量可能取多个值时尤其有用。类似于 switch 语句,但更优雅。

suspend fun setStoreImages(call: ApplicationCall) {
    val storeImagesMapper = call.receive<ArrayList<StoreImageMapper>>()
    val employeeEmail = getEmailFromJWT(call)
    if (employeeEmail == null){
      call.respond(HttpStatusCode.BadRequest, "No employee email id passed")
      return
    }
      val errorStr = StoreApis.createStoreImages(storeImagesMapper, employeeEmail)
      when(errorStr) {
        null->all.respond(HttpStatusCode.Created)
        else-> call.respond(HttpStatusCode.BadRequest, errorStr)
    }
 }
于 2021-05-23T17:12:37.723 回答
1

您可以使用扩展功能和`When Eg减少代码

suspend fun ApplicationCall.setStoreImages() {
    val storeImagesMapper = receive<ArrayList<StoreImageMapper>>()
    val employeeEmail = getEmailFromJWT(this)
    when (employeeEmail){
      null  -> respond(HttpStatusCode.BadRequest, "No employee email id passed")
    else -> {
StoreApis.createStoreImages(storeImagesMapper, employeeEmail).let {
      when (it) {
       null -> respond(HttpStatusCode.Created)
      else -> espond(HttpStatusCode.BadRequest, it!!)
      }
    } 
  }
}
  }
于 2021-05-23T17:39:40.997 回答