lenqnr
06/26/2019, 2:28 PMfun foo() {
getData()?.let { data ->
doSomething(data)
}
}
Is it equivalent to this?
fun foo() {
val data = getData() ?: return
doSomething(data)
}
I don't like adding more indentations, so came up with the second one. Is it OK?Dico
06/26/2019, 2:29 PMlenqnr
06/26/2019, 2:31 PMDico
06/26/2019, 2:33 PMval data = getData()
if (data != null) doSomething(data)
I recommend having braces for if statements always unless it's a one liner. Never do this, it is error prone (in my opinion and experience):
if (data != null)
doSomething(data)
arekolek
06/26/2019, 2:35 PMfun foo() {
getData()?.let(::doSomething)
}
lenqnr
06/26/2019, 2:40 PMreturn
is not needed.fun foo() {
bar() ?: return
doSomething1()
...
}
fun bar(): Unit? {
try {
doSomething2()
}
catch (e: SomeException) {
return null
}
...
return Unit
}
So I can handle functions in the same way whether or not they return a value. But I'm not sure if it's fine.dalexander
06/26/2019, 6:28 PMRuckus
06/26/2019, 6:30 PMBoolean
?if (!bar()) return
Dico
06/26/2019, 6:36 PMUnit?
. I wonder if you can omit the last return statementlenqnr
06/26/2019, 6:39 PMtry... catch
in every functions feels a little verbose.withContext
block. I don't know why.Dico
06/27/2019, 12:54 AM