Kotlin: Prefer clarity over conciseness

栏目: IT技术 · 发布时间: 4年前

内容简介:However, if we get too much into it, it complicates the readability of the code.

Kotlin: Prefer clarity over conciseness

Picture by Element5 Digital on Unsplash

K otlin provide us a lot of handy utilities, like takeIf etc. It could help make code so much concise, removed temporary variable etc.

However, if we get too much into it, it complicates the readability of the code.

Take for example the below.

savedStateHandle.get<String>(KEY).takeIf {
!it.isNullOrEmpty()
}?.let { setValue(it) } ?: removeValue()

One doing this might feel great as it

  • eliminates if-else,
  • chain the code together,
  • eliminate temporary variable.

However the down side of this code is,

  1. It would takes a some time for one to understand the flow of the code. It obscure the simple logic behind the code.
  2. If we decompile the code, look at the number of temporary variable it generates.
Object var2 = this.savedStateHandle.get("Key");
boolean var3 = false;
boolean var4 = false;
String it = (String)var2;
int var6 = false;
CharSequence var7 = (CharSequence)it;
boolean var8 = false;
boolean var9 = false;
String var10000 = (String)(var7 != null && var7.length() != 0 ? var2 : null);
if (var10000 != null) {
String var10 = var10000;
var3 = false;
var4 = false;
var6 = false;
Intrinsics.checkExpressionValueIsNotNull(var10, "it");
this.setValue(var10);
} else {
this.removeValue();
}

The simplified version

Let’s get back to it’s old Java way of writing, simple IF-ELSE. Doesn’t look that elegant, but one look, everything is understood.

val savedMessage = savedStateHandle.get<String>(KEY)
if (savedMessage.isNullOrBlank()) {
removeValue()
} else {
setValue(savedMessage)
}

Beside if we decompile, it looks neat too, with only little generated variables.

String savedMessage = (String)this.savedStateHandle.get("Key");
CharSequence var3 = (CharSequence)savedMessage;
boolean var4 = false;
boolean var5 = false;
if (var3 == null || StringsKt.isBlank(var3)) {
this.removeValue();
} else {
this.setValue(savedMessage);
}

Using When

Of course, if we like, we could improve it a little with when , shorten it by one line compare with IF-ELSE

val savedMessage = savedStateHandle.get<String>(KEY)
when {
savedMessage.isNullOrBlank() -> removeValue()
else -> setValue(savedMessage)
}

The decompiled code looks good too.

String savedMessage = (String)this.savedStateHandle.get("Key");
CharSequence var3 = (CharSequence)savedMessage;
boolean var4 = false;
boolean var5 = false;
if (var3 == null || StringsKt.isBlank(var3)) {
this.removeValue();
} else {
this.setValue(savedMessage);
}

Using run

But if one complaint we need to code the temporary variable savedMessage , we could eliminate it using run (or some other Scope function). Like below…

savedStateHandle.get<String?>(KEY).run {
when {
this.isNullOrBlank() -> removeValue()
else -> setValue(this)
}
}

The decompile code looks like below. Not as nice, but it’s still better than the first one.

Object var2 = this.savedStateHandle.get("Key");
boolean var3 = false;
boolean var4 = false;
String $this$run = (String)var2;
int var6 = false;
CharSequence var7 = (CharSequence)$this$run;
boolean var8 = false;
boolean var9 = false;
if (var7 == null || StringsKt.isBlank(var7)) {
this.removeValue();
} else {
this.setValue($this$run);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

高性能网站建设指南

高性能网站建设指南

Steve Souders / 刘彦博 / 电子工业出版社 / 2008年 / 35.00元

本书结合Web 2.0以来Web开发领域的最新形势和特点,介绍了网站性能问题的现状、产生的原因,以及改善或解决性能问题的原则、技术技巧和最佳实践。重点关注网页的行为特征,阐释优化Ajax、CSS、JavaScript、Flash和图片处理等要素的技术,全面涵盖浏览器端性能问题的方方面面。在《高性能网站建设指南》中,作者给出了14条具体的优化原则,每一条原则都配以范例佐证,并提供了在线支持。《高性能......一起来看看 《高性能网站建设指南》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具