Kotlins猫王胜过swift的后卫

If you’ve programmed for iOS Swift before, you’ll find this interesting keyword, guard, that helps to guard and ensure some condition is met or some variable is not null before proceeding.

如果您以前为iOS Swift编程过,则会发现这个有趣的关键字guard ,可以帮助保护并确保在继续操作之前确保满足某些条件或某些变量不为null。

func process(couldBeNullMesage: String?) {guard let notNullMessage = couldBeNullMesage else { return }    printMessage(notNullMessage)
// ... and a lot more
// things to do...
}func printMessage(message: String) {
print(message)
}

Something like the above is common, where guard is used to prevent any subsequent code from being executed if a null value is received.

上面类似的情况很常见,如果接收到null值,则使用guard来防止执行任何后续代码。

Coming from the Android and Kotlin world, I find guard fascinating. I gave a quick think about what the equivalent in Kotlin might be.

从Android和Kotlin世界的到来,我发现guard迷人。 我快速考虑了Kotlin的等效功能。

Kotlin的四种不同方式(每次都会变得更好) (Four Different Ways in Kotlin (Getting Better Each Time))

If you’re short on patience, you can go straight to the fourth approach — which amazes me.

如果您缺乏耐心,可以直接采用第四种方法,这使我感到惊讶。

流行的let (The popular let approach)

Everybody who first learns Kotlin loves let.

每个首先学习Kotlin的人都喜欢let

fun process(couldBeNullMesage: String?) {couldBeNullMesage?.let {
printMessage(it)
// ... and a lot more
// things to do...
}
}fun printMessage(message: String) {
println(message)
}

This definitely does the work. However, all the code in the function will need to have an extra indentation.

这肯定可以完成工作。 但是,该函数中的所有代码都需要额外的缩进。

As mentioned in this article, let isn’t always a good option.

正如提到的这篇文章let并不总是一个不错的选择。

2.猫王-操作者方法 (2. The Elvis-operator approach)

In Kotlin, the Elvis operator is similar to Swift coalescing — but a little smarter. It can assign a non-null value to another variable, and if it is null, it can execute return.

在Kotlin中,Elvis运算符类似于Swift合并-但更聪明。 它可以为另一个变量分配非null值,如果为null ,则可以执行return

fun process(couldBeNullMesage: String?) {    val notNullMessage = couldBeNullMesage ?: return    printMessage(notNullMessage) // this is autocast to non-null.
// ... and a lot more
// things to do...
}fun printMessage(message: String) {
println(message)
}

This is almost like the guard equivalent in the Swift language. It’s already better than guard, as it doesn’t need a parenthesis to wrap the return.

这几乎就像Swift语言中的guard一样。 它已经比guard更好,因为它不需要括号来包装return

In Swift, we can’t do the following, as it expects a valid string on the right side of coalescing — i.e., ??.

在Swift中,我们无法执行以下操作,因为它期望在合并的右侧有一个有效的字符串,即??

val notNullMessage = couldBeNullMesage ?? return

Nonetheless, I’m still not content with the Elvis-operator approach above. The conventional approach below looks even better.

尽管如此,我仍然不满意上面的猫王操作者方法。 下面的常规方法看起来更好。

3.常规的if-else方法 (3. Conventional if-else approach)

Let’s use what the Java convention teaches us: the old, faithful if-else.

让我们使用Java约定教给我们的东西:古老,忠实的if-else

fun process(couldBeNullMesage: String?) {    if (couldBeNullMesage == null) return    printMessage(couldBeNullMesage) // smartcast to non-null.
// ... and a lot more
// things to do...
}fun printMessage(message: String) {
println(message)
}

In Kotlin, the smart casting of a null-checked variable to a non-nullvariable eliminates the need for a temporary variable.

在Kotlin中,将经过null检查的变量巧妙地转换为非null变量消除了对临时变量的需要。

But wait! There’s an even better way.

可是等等! 有一个更好的方法。

4.精明的演员-Elvis操作员方法 (4. The smart cast–Elvis operator approach)

What if we combine the Elvis operator and the smart-cast capability of Kotlin together — how would it look like?

如果我们将Elvis运营商和Kotlin的智能广播功能结合在一起,会怎么样?

fun process(couldBeNullMesage: String?) {    couldBeNullMesage ?: return    printMessage(couldBeNullMesage) // smartcast to non-null.
// ... and a lot more
// things to do...
}fun printMessage(message: String) {
println(message)
}

Hallelujah! This is just genius. Concise and to the point.

哈利路亚! 这只是天才。 简明扼要。

快速比较 (A Quick Comparison)

// Swift 
guard
let notNullMessage = couldBeNullMesage else { return }// KotlincouldBeNullMesage ?: return

Sorry if I started a battle between iOS and Android. Some credit should go to iOS, as states below:

抱歉,如果我在iOS和Android之间展开战斗。 应归功于iOS,如下所示:

  • I have to thank iOS Swift for letting me ponder this by learning about guard

    我要感谢iOS Swift让我通过学习guard来思考这个问题

  • To be fair, guard isn’t just for the sole purpose of checking the null value. It’s used in other contexts (e.g., boolean variables, check for more variabless), which can’t be done with a single Kotlin Elvis operation.

    公平地讲, guard不仅是检查null值的唯一目的。 它在其他上下文中使用(例如,布尔变量,检查更多变量),这是无法通过单个Kotlin Elvis操作完成的。

It’s just in this context purely that, in my opinion, Kotlin does better.

在我看来,仅仅是在这种情况下,Kotlin的表现更好。

Thanks for reading.

谢谢阅读。

翻译自: https://medium.com/mobile-app-development-publication/kotlins-elvis-better-than-swift-s-guard-53030d403c3f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值