我什么时候应该使用 let {} 什么时候使用 != null
When should I use let {} and when just plain != null
你会如何以更 kotlinic 的方式编写以下代码?
var returnValue = ...
val s3data = presignedUrl.body()
if (s3data != null) {
val uploadImage = api.uploadImage(s3data.bucketUrl, s3data.awsAccessKeyId, s3data.policy, s3data.key, s3data.signature, body).execute()
if (!uploadImage.isSuccessful) {
crashReporterService.sendIssue("Failed uploading file", "Failed uploading file ${uploadImage.raw()}")
returnValue = Result.FAILURE
} else {
returnValue = Result.SUCCESS
}
} else {
crashReporterService.sendIssue("Failed uploading image", "Error - ${presignedUrl.raw()}")
returnValue = Result.FAILURE
}
return returnValue
我可以使用 let,但我觉得它让代码更难理解
if
/else
is an expression in Kotlin,所以下面肯定更像 Kotlinesque:
val s3data = presignedUrl.body()
return if (s3data != null) {
val uploadImage = api.uploadImage(s3data.bucketUrl, s3data.awsAccessKeyId, s3data.policy, s3data.key, s3data.signature, body).execute()
if (!uploadImage.isSuccessful) {
crashReporterService.sendIssue("Failed uploading file", "Failed uploading file ${uploadImage.raw()}")
Result.FAILURE
} else {
Result.SUCCESS
}
} else {
crashReporterService.sendIssue("Failed uploading image", "Error - ${presignedUrl.raw()}")
Result.FAILURE
}
- 公共共享代码 - 在这种情况下,错误报告和 return 失败结果 - 可以合并到 local function.
- 可空性导致 return(在这种情况下,
s3data
可以为空)通常可以用 returning ?: elvis operator. 替换
- 当一遍又一遍地键入同一个变量时(在本例中,访问
s3data
),run
block is appropriate. If confused, see
- 正如另一个答案中所述,if/else 块是 Kotlin 中的一个表达式。
因此,我会发现以下实现最理想化,需要为本地函数正确命名参数:
fun foo() {
fun failure(p0: String, p1: String) = crashReporterService.sendIssue(p0, p1).let { Result.FAILURE }
val s3data = presignedUrl.body() ?: return failure("Failed uploading image", "Error - ${presignedUrl.raw()}")
val uploadImage = s3data.run { api.uploadImage(bucketUrl, awsAccessKeyId, policy, key, signature, body).execute() }
return if (uploadImage.isSuccessful) {
Result.SUCCESS
} else {
failure("Failed uploading file", "Failed uploading file ${uploadImage.raw()}")
}
}
你的问题接近于代码审查,所以你可能也会很高兴知道之前有一个 dedicated Stack Exchange network just for that. Do read A guide to Code Review for Stack Overflow users。
你会如何以更 kotlinic 的方式编写以下代码?
var returnValue = ...
val s3data = presignedUrl.body()
if (s3data != null) {
val uploadImage = api.uploadImage(s3data.bucketUrl, s3data.awsAccessKeyId, s3data.policy, s3data.key, s3data.signature, body).execute()
if (!uploadImage.isSuccessful) {
crashReporterService.sendIssue("Failed uploading file", "Failed uploading file ${uploadImage.raw()}")
returnValue = Result.FAILURE
} else {
returnValue = Result.SUCCESS
}
} else {
crashReporterService.sendIssue("Failed uploading image", "Error - ${presignedUrl.raw()}")
returnValue = Result.FAILURE
}
return returnValue
我可以使用 let,但我觉得它让代码更难理解
if
/else
is an expression in Kotlin,所以下面肯定更像 Kotlinesque:
val s3data = presignedUrl.body()
return if (s3data != null) {
val uploadImage = api.uploadImage(s3data.bucketUrl, s3data.awsAccessKeyId, s3data.policy, s3data.key, s3data.signature, body).execute()
if (!uploadImage.isSuccessful) {
crashReporterService.sendIssue("Failed uploading file", "Failed uploading file ${uploadImage.raw()}")
Result.FAILURE
} else {
Result.SUCCESS
}
} else {
crashReporterService.sendIssue("Failed uploading image", "Error - ${presignedUrl.raw()}")
Result.FAILURE
}
- 公共共享代码 - 在这种情况下,错误报告和 return 失败结果 - 可以合并到 local function.
- 可空性导致 return(在这种情况下,
s3data
可以为空)通常可以用 returning ?: elvis operator. 替换
- 当一遍又一遍地键入同一个变量时(在本例中,访问
s3data
),run
block is appropriate. If confused, see - 正如另一个答案中所述,if/else 块是 Kotlin 中的一个表达式。
因此,我会发现以下实现最理想化,需要为本地函数正确命名参数:
fun foo() {
fun failure(p0: String, p1: String) = crashReporterService.sendIssue(p0, p1).let { Result.FAILURE }
val s3data = presignedUrl.body() ?: return failure("Failed uploading image", "Error - ${presignedUrl.raw()}")
val uploadImage = s3data.run { api.uploadImage(bucketUrl, awsAccessKeyId, policy, key, signature, body).execute() }
return if (uploadImage.isSuccessful) {
Result.SUCCESS
} else {
failure("Failed uploading file", "Failed uploading file ${uploadImage.raw()}")
}
}
你的问题接近于代码审查,所以你可能也会很高兴知道之前有一个 dedicated Stack Exchange network just for that. Do read A guide to Code Review for Stack Overflow users。