编写此代码的最佳方式是,希望减少if else的数量



有些时候我不喜欢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)
}
}
}

when语句对于减少if-else的倍数很好。在这种情况下,如果使用?.let可以获得employeeEmail,则可以提前获得errorStr

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)
}

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

让我们以你为例。我们可以使用return键跳过像这样的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)
}
}

如果其他人使用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)
}
}

您可以使用扩展函数来减少代码和`When例如

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!!)
}
} 
}
}
}

最新更新