Gyuhyeon
10/21/2019, 8:44 AMif (someObj != null) {
val someOtherLogic = repository.selectOne(someObj.field)
someOtherLogic.doThis()
someOtherLogic.doThat()
} else {
someService.someBusinessLogic()
}
apparently, checking null with if
statements is not the "kotlin way". someObj?.also {
val someOtherLogic = repository.selectOne(someObj.field)
someOtherLogic.doThis()
someOtherLogic.doThat()
} ?: run {
someService.someBusinessLogic()
}
but honestly, this looks like some serious code-smell to me. Maybe it's because I come from java, but if what the code is trying to achieve is do some business logic based on the isExists state of an object and do something else if it doesn't exist
, using ?.also{} ?: run{}
seems way worse than an explicit easy to read if / else
.
Any input would be appreciated.wasyl
10/21/2019, 8:48 AMsomeObj
is a nullable property, in which it will not be smart-cast inside the if
block.wasyl
10/21/2019, 8:49 AM?.let
, ?.also
etc. will be more readable, but if Kotlin didn’t want us to use `if`s they’d have not included it in the language 😉Gyuhyeon
10/21/2019, 8:52 AMmutable property
as nullable property
?wasyl
10/21/2019, 8:59 AMribesg
10/21/2019, 9:14 AMval someObj = someObj
before doing an if
rather than the monstruosity that is your second code blocDico
10/21/2019, 9:26 AMtoffe
10/21/2019, 10:47 AMStephan Schroeder
10/21/2019, 3:28 PM